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

interface DealerProposalDetailsAttributes {
  proposalId: string;
  requestId: string;
  proposalDocumentPath?: string;
  proposalDocumentUrl?: string;
  // costBreakup removed - now using dealer_proposal_cost_items table
  totalEstimatedBudget?: number;
  timelineMode?: 'date' | 'days';
  expectedCompletionDate?: Date;
  expectedCompletionDays?: number;
  dealerComments?: string;
  submittedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

interface DealerProposalDetailsCreationAttributes extends Optional<DealerProposalDetailsAttributes, 'proposalId' | 'proposalDocumentPath' | 'proposalDocumentUrl' | 'totalEstimatedBudget' | 'timelineMode' | 'expectedCompletionDate' | 'expectedCompletionDays' | 'dealerComments' | 'submittedAt' | 'createdAt' | 'updatedAt'> {}

class DealerProposalDetails extends Model<DealerProposalDetailsAttributes, DealerProposalDetailsCreationAttributes> implements DealerProposalDetailsAttributes {
  public proposalId!: string;
  public requestId!: string;
  public proposalDocumentPath?: string;
  public proposalDocumentUrl?: string;
  // costBreakup removed - now using dealer_proposal_cost_items table
  public totalEstimatedBudget?: number;
  public timelineMode?: 'date' | 'days';
  public expectedCompletionDate?: Date;
  public expectedCompletionDays?: number;
  public dealerComments?: string;
  public submittedAt?: Date;
  public createdAt!: Date;
  public updatedAt!: Date;

  public workflowRequest?: WorkflowRequest;
}

DealerProposalDetails.init(
  {
    proposalId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'proposal_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    proposalDocumentPath: {
      type: DataTypes.STRING(500),
      allowNull: true,
      field: 'proposal_document_path'
    },
    proposalDocumentUrl: {
      type: DataTypes.STRING(500),
      allowNull: true,
      field: 'proposal_document_url'
    },
    // costBreakup field removed - now using dealer_proposal_cost_items table
    totalEstimatedBudget: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'total_estimated_budget'
    },
    timelineMode: {
      type: DataTypes.STRING(10),
      allowNull: true,
      field: 'timeline_mode'
    },
    expectedCompletionDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'expected_completion_date'
    },
    expectedCompletionDays: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'expected_completion_days'
    },
    dealerComments: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'dealer_comments'
    },
    submittedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'submitted_at'
    },
    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: 'DealerProposalDetails',
    tableName: 'dealer_proposal_details',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        unique: true,
        fields: ['request_id']
      }
    ]
  }
);

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

WorkflowRequest.hasOne(DealerProposalDetails, {
  as: 'proposalDetails',
  foreignKey: 'requestId',
  sourceKey: 'requestId'
});

export { DealerProposalDetails };

