import {
  Column,
  CreateDateColumn,
  Entity,
  Index,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';

export type CatalogSyncTriggerType = 'manual' | 'scheduled';
export type CatalogSyncJobStatus = 'running' | 'success' | 'failed';

@Entity('catalog_sync_jobs')
@Index('idx_catalog_sync_jobs_provider_started', ['provider', 'startedAt'])
@Index('idx_catalog_sync_jobs_status_started', ['status', 'startedAt'])
export class CatalogSyncJob {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

  @Column({ type: 'varchar', length: 64 })
  provider: string;

  @Column({ name: 'trigger_type', type: 'varchar', length: 32 })
  triggerType: CatalogSyncTriggerType;

  @Column({ type: 'varchar', length: 32 })
  status: CatalogSyncJobStatus;

  @Column({ name: 'started_at', type: 'datetime', precision: 6 })
  startedAt: Date;

  @Column({ name: 'finished_at', type: 'datetime', precision: 6, nullable: true })
  finishedAt: Date | null;

  @Column({ name: 'category_total', type: 'int', default: 0 })
  categoryTotal: number;

  @Column({ name: 'product_total', type: 'int', default: 0 })
  productTotal: number;

  @Column({ name: 'category_created', type: 'int', default: 0 })
  categoryCreated: number;

  @Column({ name: 'category_updated', type: 'int', default: 0 })
  categoryUpdated: number;

  @Column({ name: 'product_created', type: 'int', default: 0 })
  productCreated: number;

  @Column({ name: 'product_updated', type: 'int', default: 0 })
  productUpdated: number;

  @Column({ name: 'product_removed', type: 'int', default: 0 })
  productRemoved: number;

  @Column({ name: 'error_message', type: 'text', nullable: true })
  errorMessage: string | null;

  @CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 6 })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'datetime', precision: 6 })
  updatedAt: Date;
}
