import { pool } from '../config/database';
import { RowDataPacket, ResultSetHeader } from 'mysql2';

export interface Favorite extends RowDataPacket {
  id: number;
  user_id: number | null;
  visitor_token: string | null;
  property_id: number;
  created_at: string;
}

export const findAllForUser = async (userId: number, connection?: any): Promise<any[]> => {
  const db = connection || pool;
  const [rows] = await db.query(`
    SELECT f.*, 
           p.reference, p.title, p.slug, p.transaction_type, p.property_type, 
           p.price, p.currency, p.surface, p.rooms, p.city, p.property_status,
           (SELECT pm.url FROM property_media pm WHERE pm.property_id = p.id AND pm.is_primary = 1 LIMIT 1) as cover_image
    FROM favorites f
    JOIN properties p ON f.property_id = p.id
    WHERE f.user_id = ?
    ORDER BY f.created_at DESC
  `, [userId]);
  return rows;
};

export const findAllForVisitor = async (visitorToken: string, connection?: any): Promise<any[]> => {
  const db = connection || pool;
  const [rows] = await db.query(`
    SELECT f.*, 
           p.reference, p.title, p.slug, p.transaction_type, p.property_type, 
           p.price, p.currency, p.surface, p.rooms, p.city, p.property_status,
           (SELECT pm.url FROM property_media pm WHERE pm.property_id = p.id AND pm.is_primary = 1 LIMIT 1) as cover_image
    FROM favorites f
    JOIN properties p ON f.property_id = p.id
    WHERE f.visitor_token = ? AND f.user_id IS NULL
    ORDER BY f.created_at DESC
  `, [visitorToken]);
  return rows;
};

export const findByPropertyAndUser = async (propertyId: number, userId: number, connection?: any): Promise<Favorite | null> => {
  const db = connection || pool;
  const [rows] = await db.query('SELECT * FROM favorites WHERE property_id = ? AND user_id = ?', [propertyId, userId]);
  return rows[0] || null;
};

export const findByPropertyAndVisitor = async (propertyId: number, visitorToken: string, connection?: any): Promise<Favorite | null> => {
  const db = connection || pool;
  const [rows] = await db.query('SELECT * FROM favorites WHERE property_id = ? AND visitor_token = ? AND user_id IS NULL', [propertyId, visitorToken]);
  return rows[0] || null;
};

export const create = async (propertyId: number, userId: number | null, visitorToken: string | null, connection?: any): Promise<number> => {
  const db = connection || pool;
  const query = 'INSERT IGNORE INTO favorites (property_id, user_id, visitor_token) VALUES (?, ?, ?)';
  const [result] = await db.query(query, [propertyId, userId, visitorToken]);
  return (result as ResultSetHeader).insertId;
};

export const remove = async (propertyId: number, userId: number | null, visitorToken: string | null, connection?: any): Promise<void> => {
  const db = connection || pool;
  if (userId) {
    await db.query('DELETE FROM favorites WHERE property_id = ? AND user_id = ?', [propertyId, userId]);
  } else if (visitorToken) {
    await db.query('DELETE FROM favorites WHERE property_id = ? AND visitor_token = ? AND user_id IS NULL', [propertyId, visitorToken]);
  }
};
