import { pool } from '../config/database';
import { RowDataPacket, ResultSetHeader } from 'mysql2';

export interface Amenity extends RowDataPacket {
  id: number;
  name: string;
  icon: string | null;
}

export const findAll = async (): Promise<Amenity[]> => {
  const [rows] = await pool.query<Amenity[]>('SELECT * FROM amenities ORDER BY name ASC');
  return rows;
};

export const findById = async (id: number, connection?: any): Promise<Amenity | null> => {
  const db = connection || pool;
  const [rows] = await db.query('SELECT * FROM amenities WHERE id = ?', [id]);
  return rows[0] || null;
};

export const findByName = async (name: string, connection?: any): Promise<Amenity | null> => {
  const db = connection || pool;
  const [rows] = await db.query('SELECT * FROM amenities WHERE name = ?', [name]);
  return rows[0] || null;
};

export const create = async (name: string, icon: string | null, connection?: any): Promise<number> => {
  const db = connection || pool;
  const [result] = await db.query('INSERT INTO amenities (name, icon) VALUES (?, ?)', [name, icon]);
  return (result as ResultSetHeader).insertId;
};

export const update = async (id: number, name?: string, icon?: string | null, connection?: any): Promise<void> => {
  const db = connection || pool;
  const updates: string[] = [];
  const params: any[] = [];
  
  if (name !== undefined) {
    updates.push('name = ?');
    params.push(name);
  }
  if (icon !== undefined) {
    updates.push('icon = ?');
    params.push(icon);
  }
  
  if (updates.length > 0) {
    params.push(id);
    await db.query(`UPDATE amenities SET ${updates.join(', ')} WHERE id = ?`, params);
  }
};

export const deleteById = async (id: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await db.query('DELETE FROM amenities WHERE id = ?', [id]);
};

// ==========================================
// Property Amenities
// ==========================================

export const getAmenitiesForProperty = async (propertyId: number): Promise<Amenity[]> => {
  const query = `
    SELECT a.* FROM amenities a
    JOIN property_amenities pa ON a.id = pa.amenity_id
    WHERE pa.property_id = ?
    ORDER BY a.name ASC
  `;
  const [rows] = await pool.query<Amenity[]>(query, [propertyId]);
  return rows;
};

export const checkPropertyAmenity = async (propertyId: number, amenityId: number, connection?: any): Promise<boolean> => {
  const db = connection || pool;
  const [rows] = await db.query('SELECT 1 FROM property_amenities WHERE property_id = ? AND amenity_id = ?', [propertyId, amenityId]);
  return rows.length > 0;
};

export const addPropertyAmenity = async (propertyId: number, amenityId: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await db.query('INSERT IGNORE INTO property_amenities (property_id, amenity_id) VALUES (?, ?)', [propertyId, amenityId]);
};

export const removePropertyAmenity = async (propertyId: number, amenityId: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await db.query('DELETE FROM property_amenities WHERE property_id = ? AND amenity_id = ?', [propertyId, amenityId]);
};

export const removeAllPropertyAmenities = async (propertyId: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await db.query('DELETE FROM property_amenities WHERE property_id = ?', [propertyId]);
};
