import { pool } from '../config/database';
import { RowDataPacket, ResultSetHeader } from 'mysql2';

export interface AcquirerProfile extends RowDataPacket {
  id: number;
  user_id: number;
  residence_project_id: number;
  unit_id: number;
  customer_reference: string | null;
  temporary_password_required: boolean;
  first_login_at: Date | null;
  status: 'ACTIVE' | 'INACTIVE';
  created_at: Date;
  updated_at: Date;
}

export const findAll = async (): Promise<AcquirerProfile[]> => {
  const [rows] = await pool.query<AcquirerProfile[]>(`
    SELECT a.*, 
           u.first_name, u.last_name, u.email, u.phone,
           ru.reference as unit_reference,
           ru.floor as unit_floor,
           ru.surface as unit_surface,
           ru.price as unit_price,
           ru.parking_option as unit_parking_option,
           rut.name as unit_typology,
           rut.code as unit_typology_code,
           rut.plan_image_url as unit_typology_plan_url,
           rp.name as project_name
    FROM acquirer_profiles a
    JOIN users u ON a.user_id = u.id
    LEFT JOIN residence_units ru ON a.unit_id = ru.id
    LEFT JOIN residence_unit_types rut ON ru.unit_type_id = rut.id
    LEFT JOIN residence_projects rp ON a.residence_project_id = rp.id
    ORDER BY a.created_at DESC
  `);
  return rows;
};

export const findById = async (id: number, connection?: any): Promise<AcquirerProfile | null> => {
  const db = connection || pool;
  const [rows] = await (db as any).query(`
    SELECT a.*, 
           u.first_name, u.last_name, u.email, u.phone,
           ru.reference as unit_reference,
           ru.price as unit_price,
           ru.floor as unit_floor,
           ru.surface as unit_surface,
           ru.parking_option as unit_parking_option,
           rut.name as unit_typology,
           rut.code as unit_typology_code,
           rut.plan_image_url as unit_typology_plan_url,
           rp.name as project_name
    FROM acquirer_profiles a
    JOIN users u ON a.user_id = u.id
    LEFT JOIN residence_units ru ON a.unit_id = ru.id
    LEFT JOIN residence_unit_types rut ON ru.unit_type_id = rut.id
    LEFT JOIN residence_projects rp ON a.residence_project_id = rp.id
    WHERE a.id = ?
  `, [id]);
  return rows[0] || null;
};

export const findByUserId = async (userId: number): Promise<AcquirerProfile[]> => {
  const [rows] = await pool.query<AcquirerProfile[]>(`
    SELECT a.*, 
           ru.reference as unit_reference,
           ru.floor as unit_floor,
           ru.surface as unit_surface,
           ru.price as unit_price,
           ru.parking_option as unit_parking_option,
           rut.name as unit_typology,
           rut.code as unit_typology_code,
           rut.plan_image_url as unit_typology_plan_url,
           rp.name as project_name
    FROM acquirer_profiles a
    LEFT JOIN residence_units ru ON a.unit_id = ru.id
    LEFT JOIN residence_unit_types rut ON ru.unit_type_id = rut.id
    LEFT JOIN residence_projects rp ON a.residence_project_id = rp.id
    WHERE a.user_id = ?
  `, [userId]);
  return rows;
};

export const findByUnitId = async (unitId: number, connection?: any): Promise<AcquirerProfile | null> => {
  const db = connection || pool;
  const [rows] = await (db as any).query('SELECT * FROM acquirer_profiles WHERE unit_id = ?', [unitId]);
  return rows[0] || null;
};

export const create = async (data: any, connection?: any): Promise<number> => {
  const db = connection || pool;
  
  const query = `
    INSERT INTO acquirer_profiles 
    (user_id, residence_project_id, unit_id, customer_reference, temporary_password_required)
    VALUES (?, ?, ?, ?, ?)
  `;
  
  const [result] = await (db as any).query(query, [
    data.user_id,
    data.residence_project_id,
    data.unit_id,
    data.customer_reference || null,
    data.temporary_password_required || false
  ]);
  
  return (result as ResultSetHeader).insertId;
};

export const update = async (id: number, data: any, connection?: any): Promise<void> => {
  const db = connection || pool;
  
  const query = `
    UPDATE acquirer_profiles 
    SET customer_reference = COALESCE(?, customer_reference),
        status = COALESCE(?, status)
    WHERE id = ?
  `;
  
  await (db as any).query(query, [
    data.customer_reference !== undefined ? data.customer_reference : null,
    data.status !== undefined ? data.status : null,
    id
  ]);
};

export const remove = async (id: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await (db as any).query('DELETE FROM acquirer_profiles WHERE id = ?', [id]);
};
