import { QueryInterface, DataTypes } from 'sequelize';
import { Sequelize } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  // Ensure uuid-ossp extension is enabled (required for uuid_generate_v4())
  await queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
  
  // Create dealers table with all fields from sample data
  await queryInterface.createTable('dealers', {
    dealer_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: Sequelize.literal('uuid_generate_v4()')
    },
    sales_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Sales Code'
    },
    service_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Service Code'
    },
    gear_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Gear Code'
    },
    gma_code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      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'
    },
    city_category_pst: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'City category (PST)'
    },
    layout_format: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Layout format'
    },
    tier_city_category: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'TIER City Category'
    },
    on_boarding_charges: {
      type: DataTypes.TEXT,
      allowNull: true,
      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)'
    },
    single_format_month_year: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Single Format of Month/Year (stored as text)'
    },
    domain_id: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Domain Id'
    },
    replacement: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Replacement (stored as text to allow longer values)'
    },
    termination_resignation_status: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Termination / Resignation under Proposal or Evaluation'
    },
    date_of_termination_resignation: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Date Of termination/ resignation (stored as text to avoid format validation)'
    },
    last_date_of_operations: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Last date of operations (stored as text to avoid format validation)'
    },
    old_codes: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Old Codes'
    },
    branch_details: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Branch Details'
    },
    dealer_principal_name: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Dealer Principal Name'
    },
    dealer_principal_email_id: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Dealer Principal Email Id'
    },
    dp_contact_number: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'DP CONTACT NUMBER (stored as text to allow multiple numbers)'
    },
    dp_contacts: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'DP CONTACTS (stored as text to allow multiple contacts)'
    },
    showroom_address: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Showroom Address'
    },
    showroom_pincode: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Showroom Pincode'
    },
    workshop_address: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Workshop Address'
    },
    workshop_pincode: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Workshop Pincode'
    },
    location_district: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Location / District'
    },
    state_workshop: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'State (for workshop)'
    },
    no_of_studios: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: 'No Of Studios'
    },
    website_update: {
      type: DataTypes.TEXT,
      allowNull: true,
      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'
    },
    firm_type: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Firm Type'
    },
    prop_managing_partners_directors: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Prop. / Managing Partners / Managing Directors'
    },
    total_prop_partners_directors: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Total Prop. / Partners / Directors'
    },
    docs_folder_link: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'DOCS Folder Link'
    },
    workshop_gma_codes: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Workshop GMA Codes'
    },
    existing_new: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Existing / New'
    },
    dlrcode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'dlrcode'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: 'Whether the dealer is currently active'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
    }
  });

  // Create indexes
  await queryInterface.addIndex('dealers', ['sales_code'], {
    name: 'idx_dealers_sales_code',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['service_code'], {
    name: 'idx_dealers_service_code',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['gma_code'], {
    name: 'idx_dealers_gma_code',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['domain_id'], {
    name: 'idx_dealers_domain_id',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['region'], {
    name: 'idx_dealers_region',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['state'], {
    name: 'idx_dealers_state',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['city'], {
    name: 'idx_dealers_city',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['district'], {
    name: 'idx_dealers_district',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['dlrcode'], {
    name: 'idx_dealers_dlrcode',
    unique: false
  });

  await queryInterface.addIndex('dealers', ['is_active'], {
    name: 'idx_dealers_is_active',
    unique: false
  });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  // Drop indexes first
  await queryInterface.removeIndex('dealers', 'idx_dealers_sales_code');
  await queryInterface.removeIndex('dealers', 'idx_dealers_service_code');
  await queryInterface.removeIndex('dealers', 'idx_dealers_gma_code');
  await queryInterface.removeIndex('dealers', 'idx_dealers_domain_id');
  await queryInterface.removeIndex('dealers', 'idx_dealers_region');
  await queryInterface.removeIndex('dealers', 'idx_dealers_state');
  await queryInterface.removeIndex('dealers', 'idx_dealers_city');
  await queryInterface.removeIndex('dealers', 'idx_dealers_district');
  await queryInterface.removeIndex('dealers', 'idx_dealers_dlrcode');
  await queryInterface.removeIndex('dealers', 'idx_dealers_is_active');

  // Drop table
  await queryInterface.dropTable('dealers');
}

