import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  // 1. Create dealer_claim_details table
  await queryInterface.createTable('dealer_claim_details', {
    claim_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    },
    activity_name: {
      type: DataTypes.STRING(500),
      allowNull: false
    },
    activity_type: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    dealer_code: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    dealer_name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    dealer_email: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    dealer_phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    dealer_address: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    activity_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    location: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    period_start_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    period_end_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  });

  // Create indexes
  await queryInterface.addIndex('dealer_claim_details', ['request_id'], {
    name: 'idx_dealer_claim_details_request_id',
    unique: true
  });
  await queryInterface.addIndex('dealer_claim_details', ['dealer_code'], {
    name: 'idx_dealer_claim_details_dealer_code'
  });
  await queryInterface.addIndex('dealer_claim_details', ['activity_type'], {
    name: 'idx_dealer_claim_details_activity_type'
  });

  // 2. Create dealer_proposal_details table (Step 1: Dealer Proposal)
  await queryInterface.createTable('dealer_proposal_details', {
    proposal_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    },
    proposal_document_path: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    proposal_document_url: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    total_estimated_budget: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true
    },
    timeline_mode: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    expected_completion_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    expected_completion_days: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    dealer_comments: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  });

  await queryInterface.addIndex('dealer_proposal_details', ['request_id'], {
    name: 'idx_dealer_proposal_details_request_id',
    unique: true
  });

  // 3. Create dealer_completion_details table (Step 5: Dealer Completion)
  await queryInterface.createTable('dealer_completion_details', {
    completion_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    },
    activity_completion_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    number_of_participants: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    total_closed_expenses: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  });

  await queryInterface.addIndex('dealer_completion_details', ['request_id'], {
    name: 'idx_dealer_completion_details_request_id',
    unique: true
  });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('dealer_completion_details');
  await queryInterface.dropTable('dealer_proposal_details');
  await queryInterface.dropTable('dealer_claim_details');
}

