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

interface DealerClaimDetailsAttributes {
  claimId: string;
  requestId: string;
  activityName: string;
  activityType: string;
  dealerCode: string;
  dealerName: string;
  dealerEmail?: string;
  dealerPhone?: string;
  dealerAddress?: string | null;
  activityDate?: Date | null;
  location?: string;
  periodStartDate?: Date;
  periodEndDate?: Date;
  totalProposedTaxableAmount?: number;
  createdAt: Date;
  updatedAt: Date;
}

interface DealerClaimDetailsCreationAttributes extends Optional<DealerClaimDetailsAttributes, 'claimId' | 'dealerEmail' | 'dealerPhone' | 'dealerAddress' | 'activityDate' | 'location' | 'periodStartDate' | 'periodEndDate' | 'totalProposedTaxableAmount' | 'createdAt' | 'updatedAt'> { }

class DealerClaimDetails extends Model<DealerClaimDetailsAttributes, DealerClaimDetailsCreationAttributes> implements DealerClaimDetailsAttributes {
  public claimId!: string;
  public requestId!: string;
  public activityName!: string;
  public activityType!: string;
  public dealerCode!: string;
  public dealerName!: string;
  public dealerEmail?: string;
  public dealerPhone?: string;
  public dealerAddress?: string | null;
  public activityDate?: Date | null;
  public location?: string;
  public periodStartDate?: Date;
  public periodEndDate?: Date;
  public totalProposedTaxableAmount?: number;
  public createdAt!: Date;
  public updatedAt!: Date;

  // Associations
  public workflowRequest?: WorkflowRequest;
}

DealerClaimDetails.init(
  {
    claimId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'claim_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    activityName: {
      type: DataTypes.STRING(500),
      allowNull: false,
      field: 'activity_name'
    },
    activityType: {
      type: DataTypes.STRING(100),
      allowNull: false,
      field: 'activity_type'
    },
    dealerCode: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'dealer_code'
    },
    dealerName: {
      type: DataTypes.STRING(200),
      allowNull: false,
      field: 'dealer_name'
    },
    dealerEmail: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'dealer_email'
    },
    dealerPhone: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'dealer_phone'
    },
    dealerAddress: {
      type: DataTypes.TEXT,
      field: 'dealer_address',
      comment: 'Dealer address'
    },
    activityDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'activity_date'
    },
    location: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    periodStartDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'period_start_date'
    },
    periodEndDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'period_end_date'
    },
    totalProposedTaxableAmount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'total_proposed_taxable_amount',
      comment: 'Total taxable amount from proposal or actuals if higher'
    },
    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: 'DealerClaimDetails',
    tableName: 'dealer_claim_details',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        unique: true,
        fields: ['request_id']
      },
      {
        fields: ['dealer_code']
      },
      {
        fields: ['activity_type']
      }
    ]
  }
);

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

WorkflowRequest.hasOne(DealerClaimDetails, {
  as: 'claimDetails',
  foreignKey: 'requestId',
  sourceKey: 'requestId'
});

export { DealerClaimDetails };

