import {
  Table,
  Model,
  Column,
  DataType,
  CreatedAt,
  UpdatedAt,
  BelongsTo,
  ForeignKey,
} from 'sequelize-typescript';
import { CustomerBaseModel } from './customer-base.entity';
import { EventTypeEnum } from '../enum/event-type.enum';
import { SyncStatusEnum } from '../enum/sync-status.enum';

@Table({ tableName: 'customer_SyncLog', timestamps: true })
export class CustomerSyncLogModel extends Model {
  @Column({ primaryKey: true, allowNull: false, type: DataType.STRING(30) })
  declare SyncLogId: string;

  @ForeignKey(() => CustomerBaseModel)
  @Column({ allowNull: false, type: DataType.STRING(30) })
  declare CustomerId: string;

  @Column({ allowNull: false, type: DataType.STRING(5) })
  declare SourceSystemCode: string;

  @Column({ allowNull: false, type: DataType.STRING(5) })
  declare TargetSystemCode: string;

  @Column({ allowNull: true, type: DataType.STRING(30) })
  declare EventId: string;

  @Column({
    allowNull: true,
    type: DataType.ENUM(...Object.values(EventTypeEnum)),
  })
  declare EventType: EventTypeEnum;

  @Column({
    allowNull: true,
    type: DataType.ENUM(...Object.values(SyncStatusEnum)),
  })
  declare Status: SyncStatusEnum;

  @Column({ allowNull: true, type: DataType.JSON })
  declare PayloadSnapshot: string;

  @Column({
    allowNull: false,
    type: DataType.STRING(10),
  })
  declare TrigerredBy: string;

  @Column({
    allowNull: true,
    type: DataType.STRING(30),
  })
  declare QueueJobId: string;

  @Column({
    allowNull: true,
    type: DataType.DATE,
  })
  declare SyncedAt: Date;

  @CreatedAt
  CreatedAt: Date;

  @UpdatedAt
  declare UpdatedAt: Date;

  @Column({ allowNull: true, type: DataType.INTEGER })
  declare RetryCount: number;

  @Column({ allowNull: true, type: DataType.TEXT })
  declare LastErrorMessage: string;

  @Column({ allowNull: true, type: DataType.DATE })
  declare LastAttemptAt: Date;

  @Column({ allowNull: true, type: DataType.STRING(10) })
  declare LastStatus: string;

  @BelongsTo(() => CustomerBaseModel)
  declare CustomerBase: CustomerBaseModel;
}
