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

interface DealerProposalCostItemAttributes {
  costItemId: string;
  proposalId: string;
  requestId: string;
  itemDescription: string;
  amount: number;
  itemOrder: number;
  createdAt: Date;
  updatedAt: Date;
}

interface DealerProposalCostItemCreationAttributes extends Optional<DealerProposalCostItemAttributes, 'costItemId' | 'itemOrder' | 'createdAt' | 'updatedAt'> {}

class DealerProposalCostItem extends Model<DealerProposalCostItemAttributes, DealerProposalCostItemCreationAttributes> implements DealerProposalCostItemAttributes {
  public costItemId!: string;
  public proposalId!: string;
  public requestId!: string;
  public itemDescription!: string;
  public amount!: number;
  public itemOrder!: number;
  public createdAt!: Date;
  public updatedAt!: Date;

  // Associations
  public proposal?: DealerProposalDetails;
  public workflowRequest?: WorkflowRequest;
}

DealerProposalCostItem.init(
  {
    costItemId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'cost_item_id'
    },
    proposalId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'proposal_id',
      references: {
        model: 'dealer_proposal_details',
        key: 'proposal_id'
      }
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    itemDescription: {
      type: DataTypes.STRING(500),
      allowNull: false,
      field: 'item_description'
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false
    },
    itemOrder: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      field: 'item_order'
    },
    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: 'DealerProposalCostItem',
    tableName: 'dealer_proposal_cost_items',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['proposal_id'], name: 'idx_proposal_cost_items_proposal_id' },
      { fields: ['request_id'], name: 'idx_proposal_cost_items_request_id' },
      { fields: ['proposal_id', 'item_order'], name: 'idx_proposal_cost_items_proposal_order' }
    ]
  }
);

// Associations
DealerProposalCostItem.belongsTo(DealerProposalDetails, {
  as: 'proposal',
  foreignKey: 'proposalId',
  targetKey: 'proposalId'
});

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

DealerProposalDetails.hasMany(DealerProposalCostItem, {
  as: 'costItems',
  foreignKey: 'proposalId',
  sourceKey: 'proposalId'
});

export { DealerProposalCostItem };

