import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';
import { User } from './User';

export interface Form16NonSubmittedNotificationAttributes {
  id: number;
  dealerCode: string;
  financialYear: string;
  notifiedAt: Date;
  notifiedBy: string;
}

interface Form16NonSubmittedNotificationCreationAttributes
  extends Optional<Form16NonSubmittedNotificationAttributes, 'id' | 'notifiedAt'> {}

class Form16NonSubmittedNotification
  extends Model<Form16NonSubmittedNotificationAttributes, Form16NonSubmittedNotificationCreationAttributes>
  implements Form16NonSubmittedNotificationAttributes
{
  public id!: number;
  public dealerCode!: string;
  public financialYear!: string;
  public notifiedAt!: Date;
  public notifiedBy!: string;

  public notifiedByUser?: User;
}

Form16NonSubmittedNotification.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    dealerCode: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'dealer_code',
    },
    financialYear: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'financial_year',
    },
    notifiedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'notified_at',
    },
    notifiedBy: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'notified_by',
      references: { model: 'users', key: 'user_id' },
    },
  },
  {
    sequelize,
    tableName: 'form16_non_submitted_notifications',
    timestamps: false,
  }
);

Form16NonSubmittedNotification.belongsTo(User, {
  as: 'notifiedByUser',
  foreignKey: 'notifiedBy',
  targetKey: 'userId',
});

export { Form16NonSubmittedNotification };
