import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  protected tableName = 'international_orders'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.integer('order_id').unsigned().references('id').inTable('orders').onDelete('CASCADE')

      // Shipper Details
      table.string('shipper_company_name').nullable()
      table.string('shipper_name').nullable()
      table.string('shipper_address').nullable()
      table.string('shipper_city').nullable()
      table.string('shipper_postal_code').nullable()
      table.string('shipper_country').nullable()
      table.string('shipper_phone').nullable()
      table.string('shipper_email').nullable()
      table.string('shipper_tax_no').nullable()

      // Recipient Details
      table.string('recipient_company_name').nullable()
      table.string('recipient_name').nullable()
      table.string('recipient_address').nullable()
      table.string('recipient_city').nullable()
      table.string('recipient_postal_code').nullable()
      table.string('recipient_country').nullable()
      table.string('recipient_phone').nullable()
      table.string('recipient_mobile').nullable()
      table.string('recipient_email').nullable()
      table.string('recipient_tax_no').nullable()

      // Shipment Details
      table.string('currency').nullable()
      table.decimal('customs_value', 12, 3).nullable()
      table.string('dhl_account_number').nullable()
      table.string('api_key').nullable()
      table.string('account_number').nullable()
      table.string('account_pin').nullable()
      table.string('meter_number').nullable()
      table.string('account_entity').nullable()
      table.string('account_country_code').nullable()
      table.string('product_group').nullable()
      table.string('product_type').nullable()
      table.string('payment_type').nullable()
      table.string('packaging_type').nullable()
      table.string('carrier').nullable()
      table.string('description').nullable()

      table
        .integer('shipper_city_id')
        .unsigned()
        .references('id')
        .inTable('international_cities')
        .onDelete('SET NULL')
        .nullable()
      table
        .integer('shipper_country_id')
        .unsigned()
        .references('id')
        .inTable('master_data_countries')
        .onDelete('SET NULL')
        .nullable()
      table
        .integer('recipient_city_id')
        .unsigned()
        .references('id')
        .inTable('international_cities')
        .onDelete('SET NULL')
        .nullable()
      table
        .integer('recipient_country_id')
        .unsigned()
        .references('id')
        .inTable('master_data_countries')
        .onDelete('SET NULL')
        .nullable()

      table.string('courier_order_tracking_no').nullable()
      table.text('courier_order_barcode_url', 'longtext').collate('utf8mb4_unicode_ci').nullable()
      table.text('shipper_response').nullable()
      table.string('service_code').nullable()
      table.decimal('courier_rate', 12, 3).nullable()
      table.decimal('margin', 12, 3).nullable()

      table.timestamp('created_at').notNullable()
      table.timestamp('updated_at').nullable()
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
