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

interface DealerAttributes {
  dealerId: string;
  salesCode?: string | null;
  serviceCode?: string | null;
  gearCode?: string | null;
  gmaCode?: string | null;
  region?: string | null;
  dealership?: string | null;
  state?: string | null;
  district?: string | null;
  city?: string | null;
  location?: string | null;
  cityCategoryPst?: string | null;
  layoutFormat?: string | null;
  tierCityCategory?: string | null;
  onBoardingCharges?: string | null;
  date?: string | null;
  singleFormatMonthYear?: string | null;
  domainId?: string | null;
  replacement?: string | null;
  terminationResignationStatus?: string | null;
  dateOfTerminationResignation?: string | null;
  lastDateOfOperations?: string | null;
  oldCodes?: string | null;
  branchDetails?: string | null;
  dealerPrincipalName?: string | null;
  dealerPrincipalEmailId?: string | null;
  dpContactNumber?: string | null;
  dpContacts?: string | null;
  showroomAddress?: string | null;
  showroomPincode?: string | null;
  workshopAddress?: string | null;
  workshopPincode?: string | null;
  locationDistrict?: string | null;
  stateWorkshop?: string | null;
  noOfStudios?: number | null;
  websiteUpdate?: string | null;
  gst?: string | null;
  pan?: string | null;
  firmType?: string | null;
  propManagingPartnersDirectors?: string | null;
  totalPropPartnersDirectors?: string | null;
  docsFolderLink?: string | null;
  workshopGmaCodes?: string | null;
  existingNew?: string | null;
  dlrcode?: string | null;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}

interface DealerCreationAttributes extends Optional<DealerAttributes, 'dealerId' | 'isActive' | 'createdAt' | 'updatedAt'> {}

class Dealer extends Model<DealerAttributes, DealerCreationAttributes> implements DealerAttributes {
  public dealerId!: string;
  public salesCode?: string | null;
  public serviceCode?: string | null;
  public gearCode?: string | null;
  public gmaCode?: string | null;
  public region?: string | null;
  public dealership?: string | null;
  public state?: string | null;
  public district?: string | null;
  public city?: string | null;
  public location?: string | null;
  public cityCategoryPst?: string | null;
  public layoutFormat?: string | null;
  public tierCityCategory?: string | null;
  public onBoardingCharges?: string | null;
  public date?: string | null;
  public singleFormatMonthYear?: string | null;
  public domainId?: string | null;
  public replacement?: string | null;
  public terminationResignationStatus?: string | null;
  public dateOfTerminationResignation?: string | null;
  public lastDateOfOperations?: string | null;
  public oldCodes?: string | null;
  public branchDetails?: string | null;
  public dealerPrincipalName?: string | null;
  public dealerPrincipalEmailId?: string | null;
  public dpContactNumber?: string | null;
  public dpContacts?: string | null;
  public showroomAddress?: string | null;
  public showroomPincode?: string | null;
  public workshopAddress?: string | null;
  public workshopPincode?: string | null;
  public locationDistrict?: string | null;
  public stateWorkshop?: string | null;
  public noOfStudios?: number | null;
  public websiteUpdate?: string | null;
  public gst?: string | null;
  public pan?: string | null;
  public firmType?: string | null;
  public propManagingPartnersDirectors?: string | null;
  public totalPropPartnersDirectors?: string | null;
  public docsFolderLink?: string | null;
  public workshopGmaCodes?: string | null;
  public existingNew?: string | null;
  public dlrcode?: string | null;
  public isActive!: boolean;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

Dealer.init(
  {
    dealerId: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
      field: 'dealer_id'
    },
    salesCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'sales_code',
      comment: 'Sales Code'
    },
    serviceCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'service_code',
      comment: 'Service Code'
    },
    gearCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'gear_code',
      comment: 'Gear Code'
    },
    gmaCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'gma_code',
      comment: 'GMA CODE'
    },
    region: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Region'
    },
    dealership: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Dealership name'
    },
    state: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'State'
    },
    district: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'District'
    },
    city: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'City'
    },
    location: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Location'
    },
    cityCategoryPst: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'city_category_pst',
      comment: 'City category (PST)'
    },
    layoutFormat: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'layout_format',
      comment: 'Layout format'
    },
    tierCityCategory: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'tier_city_category',
      comment: 'TIER City Category'
    },
    onBoardingCharges: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'on_boarding_charges',
      comment: 'On Boarding Charges (stored as text to allow text values)'
    },
    date: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'DATE (stored as text to avoid format validation)'
    },
    singleFormatMonthYear: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'single_format_month_year',
      comment: 'Single Format of Month/Year (stored as text)'
    },
    domainId: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'domain_id',
      comment: 'Domain Id'
    },
    replacement: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Replacement (stored as text to allow longer values)'
    },
    terminationResignationStatus: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'termination_resignation_status',
      comment: 'Termination / Resignation under Proposal or Evaluation'
    },
    dateOfTerminationResignation: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'date_of_termination_resignation',
      comment: 'Date Of termination/ resignation (stored as text to avoid format validation)'
    },
    lastDateOfOperations: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'last_date_of_operations',
      comment: 'Last date of operations (stored as text to avoid format validation)'
    },
    oldCodes: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'old_codes',
      comment: 'Old Codes'
    },
    branchDetails: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'branch_details',
      comment: 'Branch Details'
    },
    dealerPrincipalName: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'dealer_principal_name',
      comment: 'Dealer Principal Name'
    },
    dealerPrincipalEmailId: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'dealer_principal_email_id',
      comment: 'Dealer Principal Email Id'
    },
    dpContactNumber: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'dp_contact_number',
      comment: 'DP CONTACT NUMBER (stored as text to allow multiple numbers)'
    },
    dpContacts: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'dp_contacts',
      comment: 'DP CONTACTS (stored as text to allow multiple contacts)'
    },
    showroomAddress: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'showroom_address',
      comment: 'Showroom Address'
    },
    showroomPincode: {
      type: DataTypes.STRING(10),
      allowNull: true,
      field: 'showroom_pincode',
      comment: 'Showroom Pincode'
    },
    workshopAddress: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'workshop_address',
      comment: 'Workshop Address'
    },
    workshopPincode: {
      type: DataTypes.STRING(10),
      allowNull: true,
      field: 'workshop_pincode',
      comment: 'Workshop Pincode'
    },
    locationDistrict: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'location_district',
      comment: 'Location / District'
    },
    stateWorkshop: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'state_workshop',
      comment: 'State (for workshop)'
    },
    noOfStudios: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      field: 'no_of_studios',
      comment: 'No Of Studios'
    },
    websiteUpdate: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'website_update',
      comment: 'Website update (stored as text to allow longer values)'
    },
    gst: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'GST'
    },
    pan: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'PAN'
    },
    firmType: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'firm_type',
      comment: 'Firm Type'
    },
    propManagingPartnersDirectors: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'prop_managing_partners_directors',
      comment: 'Prop. / Managing Partners / Managing Directors'
    },
    totalPropPartnersDirectors: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'total_prop_partners_directors',
      comment: 'Total Prop. / Partners / Directors'
    },
    docsFolderLink: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'docs_folder_link',
      comment: 'DOCS Folder Link'
    },
    workshopGmaCodes: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'workshop_gma_codes',
      comment: 'Workshop GMA Codes'
    },
    existingNew: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'existing_new',
      comment: 'Existing / New'
    },
    dlrcode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'dlrcode'
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      field: 'is_active',
      comment: 'Whether the dealer is currently active'
    },
    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,
    tableName: 'dealers',
    modelName: 'Dealer',
    timestamps: true,
    underscored: true,
    indexes: [
      {
        fields: ['sales_code'],
        name: 'idx_dealers_sales_code'
      },
      {
        fields: ['service_code'],
        name: 'idx_dealers_service_code'
      },
      {
        fields: ['gma_code'],
        name: 'idx_dealers_gma_code'
      },
      {
        fields: ['domain_id'],
        name: 'idx_dealers_domain_id'
      },
      {
        fields: ['region'],
        name: 'idx_dealers_region'
      },
      {
        fields: ['state'],
        name: 'idx_dealers_state'
      },
      {
        fields: ['city'],
        name: 'idx_dealers_city'
      },
      {
        fields: ['district'],
        name: 'idx_dealers_district'
      },
      {
        fields: ['dlrcode'],
        name: 'idx_dealers_dlrcode'
      },
      {
        fields: ['is_active'],
        name: 'idx_dealers_is_active'
      }
    ]
  }
);

export { Dealer };
export type { DealerAttributes, DealerCreationAttributes };
