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

