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

interface DealerCompletionExpenseAttributes {
  expenseId: string;
  requestId: string;
  completionId?: string | null;
  description: string;
  amount: number;
  gstRate?: number;
  gstAmt?: number;
  quantity?: number;
  hsnCode?: string;
  cgstRate?: number;
  cgstAmt?: number;
  sgstRate?: number;
  sgstAmt?: number;
  igstRate?: number;
  igstAmt?: number;
  utgstRate?: number;
  utgstAmt?: number;
  cessRate?: number;
  cessAmt?: number;
  totalAmt?: number;
  isService?: boolean;
  expenseDate: Date;
  createdAt: Date;
  updatedAt: Date;
}

interface DealerCompletionExpenseCreationAttributes extends Optional<DealerCompletionExpenseAttributes, 'expenseId' | 'completionId' | 'createdAt' | 'updatedAt'> { }

class DealerCompletionExpense extends Model<DealerCompletionExpenseAttributes, DealerCompletionExpenseCreationAttributes> implements DealerCompletionExpenseAttributes {
  public expenseId!: string;
  public requestId!: string;
  public completionId?: string | null;
  public description!: string;
  public amount!: number;
  public gstRate?: number;
  public gstAmt?: number;
  public quantity?: number;
  public hsnCode?: string;
  public cgstRate?: number;
  public cgstAmt?: number;
  public sgstRate?: number;
  public sgstAmt?: number;
  public igstRate?: number;
  public igstAmt?: number;
  public utgstRate?: number;
  public utgstAmt?: number;
  public cessRate?: number;
  public cessAmt?: number;
  public totalAmt?: number;
  public isService?: boolean;
  public expenseDate!: Date;
  public createdAt!: Date;
  public updatedAt!: Date;
}

DealerCompletionExpense.init(
  {
    expenseId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'expense_id',
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id',
      },
    },
    completionId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'completion_id',
      references: {
        model: 'dealer_completion_details',
        key: 'completion_id',
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: false,
      field: 'description',
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      field: 'amount',
    },
    gstRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'gst_rate'
    },
    gstAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'gst_amt'
    },
    quantity: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 1,
      field: 'quantity'
    },
    hsnCode: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'hsn_code'
    },
    cgstRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'cgst_rate'
    },
    cgstAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'cgst_amt'
    },
    sgstRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'sgst_rate'
    },
    sgstAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'sgst_amt'
    },
    igstRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'igst_rate'
    },
    igstAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'igst_amt'
    },
    utgstRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'utgst_rate'
    },
    utgstAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'utgst_amt'
    },
    cessRate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      field: 'cess_rate'
    },
    cessAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'cess_amt'
    },
    totalAmt: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'total_amt'
    },
    isService: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: false,
      field: 'is_service'
    },
    expenseDate: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'expense_date',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'updated_at',
    },
  },
  {
    sequelize,
    modelName: 'DealerCompletionExpense',
    tableName: 'dealer_completion_expenses',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['request_id'], name: 'idx_dealer_completion_expenses_request_id' },
      { fields: ['completion_id'], name: 'idx_dealer_completion_expenses_completion_id' },
    ],
  }
);

WorkflowRequest.hasMany(DealerCompletionExpense, {
  as: 'completionExpenses',
  foreignKey: 'requestId',
  sourceKey: 'requestId',
});

DealerCompletionExpense.belongsTo(WorkflowRequest, {
  as: 'workflowRequest',
  foreignKey: 'requestId',
  targetKey: 'requestId',
});

DealerCompletionDetails.hasMany(DealerCompletionExpense, {
  as: 'expenses',
  foreignKey: 'completionId',
  sourceKey: 'completionId',
});

DealerCompletionExpense.belongsTo(DealerCompletionDetails, {
  as: 'completion',
  foreignKey: 'completionId',
  targetKey: 'completionId',
});

export { DealerCompletionExpense };

