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;
  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 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',
    },
    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 };

