module.exports = (sequelize, DataTypes) => {
  const BusinessPartner = sequelize.define('BusinessPartner', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    bp_number: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    bp_type: {
      type: DataTypes.ENUM('CUSTOMER', 'VENDOR', 'BOTH'),
      allowNull: false
    },
    name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    legal_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    vendor_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Vendor name for printing purposes'
    },
    pan: {
      type: DataTypes.STRING(16),
      allowNull: true
    },
    aadhaar: {
      type: DataTypes.STRING(12),
      allowNull: true
    },
    registration_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    registration_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    tax_category: {
      type: DataTypes.ENUM('REGULAR', 'COMPOSITION', 'UNREGISTERED', 'SEZ', 'DEEMED_EXPORT'),
      allowNull: true
    },
    payment_terms: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Payment terms in days'
    },
    credit_limit: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      defaultValue: 0.00
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    gl_account_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GLAccount',
        key: 'id'
      }
    },
    customer_advance_balance: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    vendor_advance_balance: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    vendor_category_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'VendorCategory',
        key: 'id'
      },
      comment: 'Reference to Vendor Category master (e.g., Regular Vendor, Sub Contractor, Job Worker)'
    },
    vendor_type_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'VendorCategory',
        key: 'id'
      },
      comment: 'Alias for vendor_category_id - used for filtering vendors by type/category'
    },
    // Supplier Registration Fields (for Job Worker vendors)
    year_of_establishment: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Year of establishment'
    },
    total_area_sqft: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Total area in square feet'
    },
    built_up_area_sqft: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Built-up area in square feet'
    },
    building_type: {
      type: DataTypes.ENUM('OWN', 'RENTED'),
      allowNull: true,
      comment: 'Own Building or Rented Building'
    },
    nature_of_business: {
      type: DataTypes.ENUM('MANUFACTURER', 'DEALER', 'AGENCY', 'JOB_ORDER'),
      allowNull: true,
      comment: 'Nature of Business: Manufacturer / Dealer / Agency / Job-Order'
    },
    nature_of_manufacturing_service: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Nature of Manufacturing / Service'
    },
    company_type: {
      type: DataTypes.ENUM('PUBLIC', 'PRIVATE_LTD', 'PARTNERSHIP', 'PROPRIETORY'),
      allowNull: true,
      comment: 'Type of Company: Public / Private Ltd / Partnership / Proprietory'
    },
    phone_numbers: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Phone Number(s)'
    },
    fax_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Fax number'
    },
    md_partner_proprietor_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'MD / Partner / Proprietor Name(s)'
    },
    experience_in_field: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Experience in the field'
    },
    handling_facilities: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Available handling facilities (Transport etc.)'
    },
    capacity_plant: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Capacity - Plant'
    },
    capacity_installed: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Capacity - Installed'
    },
    capacity_spare: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      comment: 'Capacity - Spare'
    },
    generator_available: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      comment: 'Is Generator available?'
    },
    generator_capacity: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Generator Capacity if available'
    },
    machinery_details: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Details of Machinery'
    },
    measuring_equipments_details: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Details of Measuring Equipments'
    },
    employees_technical_staff: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Number of Technical Staff'
    },
    employees_skilled_workers: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Number of Skilled Workers'
    },
    employees_non_technical_staff: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Number of Non-Technical Staff'
    },
    employees_unskilled_workers: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Number of Unskilled Workers'
    },
    working_hours: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Working hours'
    },
    number_of_shifts: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Number of Shifts'
    },
    shift_timings: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Shift timings'
    },
    iso_9001_certified: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      comment: 'Is the Company Certified for ISO 9001?'
    },
    iso_9001_certification_planned: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      comment: 'If No, Is there any plan to get Certification?'
    },
    iso_9001_certification_target: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'If Yes, Target date for ISO 9001 certification'
    },
    major_customer: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Major Customer'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'BLOCKED', 'BLACKLISTED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    approval_status: {
      type: DataTypes.ENUM('PENDING', 'APPROVED', 'REJECTED'),
      allowNull: false,
      defaultValue: 'PENDING',
      comment: 'Approval status for business partner'
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who approved the business partner'
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Date and time when business partner was approved'
    },
    rejected_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who rejected the business partner'
    },
    rejected_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Date and time when business partner was rejected'
    },
    rejection_reason: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Reason for rejection'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'business_partner',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'bp_number']
      },
      {
        fields: ['bp_type']
      },
      {
        fields: ['status']
      },
      {
        fields: ['pan']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  BusinessPartner.associate = function(models) {
    BusinessPartner.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    BusinessPartner.hasMany(models.BPAddress, { foreignKey: 'bp_id', as: 'addresses' });
    BusinessPartner.hasMany(models.BPBank, { foreignKey: 'bp_id', as: 'banks' });
    BusinessPartner.hasMany(models.BPGSTIN, { foreignKey: 'bp_id', as: 'gstins' });
    BusinessPartner.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    BusinessPartner.belongsTo(models.GLAccount, { foreignKey: 'gl_account_id', as: 'glAccount' });
    BusinessPartner.belongsTo(models.VendorCategory, { foreignKey: 'vendor_category_id', as: 'vendorCategory' });
    BusinessPartner.hasMany(models.PurchaseCosting, { foreignKey: 'vendor_id', as: 'purchaseCostings' });
  };

  return BusinessPartner;
};

