import { pool } from '../config/database';
import { RowDataPacket, ResultSetHeader } from 'mysql2';

export interface ResidenceProject extends RowDataPacket {
  id: number;
  name: string;
  slug: string;
  description: string;
  address: string;
  city: string;
  latitude: number | null;
  longitude: number | null;
  status: 'PLANNED' | 'UNDER_CONSTRUCTION' | 'COMPLETED';
  start_date: Date | null;
  expected_end_date: Date | null;
  cover_image: string | null;
  brochure_url: string | null;
  architectural_plan_url: string | null;
  created_at: Date;
  updated_at: Date;
}

export const findAll = async (): Promise<ResidenceProject[]> => {
  const [rows] = await pool.query<ResidenceProject[]>('SELECT * FROM residence_projects ORDER BY created_at DESC');
  return rows;
};

export const findById = async (id: number): Promise<ResidenceProject | null> => {
  const [rows] = await pool.query<ResidenceProject[]>('SELECT * FROM residence_projects WHERE id = ?', [id]);
  return rows[0] || null;
};

export const findBySlug = async (slug: string): Promise<ResidenceProject | null> => {
  const [rows] = await pool.query<ResidenceProject[]>('SELECT * FROM residence_projects WHERE slug = ?', [slug]);
  return rows[0] || null;
};

export const create = async (data: any, connection?: any): Promise<number> => {
  const db = connection || pool;
  
  const query = `
    INSERT INTO residence_projects 
    (name, slug, description, address, city, latitude, longitude, status, start_date, expected_end_date, cover_image, brochure_url, architectural_plan_url)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  `;
  
  const [result] = await (db as any).query(query, [
    data.name,
    data.slug,
    data.description || null,
    data.address || null,
    data.city,
    data.latitude || null,
    data.longitude || null,
    data.status,
    data.start_date || null,
    data.expected_end_date || null,
    data.cover_image !== undefined ? data.cover_image : null,
    data.brochure_url !== undefined ? data.brochure_url : null,
    data.architectural_plan_url !== undefined ? data.architectural_plan_url : null
  ]);
  
  return (result as ResultSetHeader).insertId;
};

export const update = async (id: number, data: any, connection?: any): Promise<void> => {
  const db = connection || pool;
  
  const query = `
    UPDATE residence_projects 
    SET name = COALESCE(?, name),
        slug = COALESCE(?, slug),
        description = COALESCE(?, description),
        address = COALESCE(?, address),
        city = COALESCE(?, city),
        latitude = COALESCE(?, latitude),
        longitude = COALESCE(?, longitude),
        status = COALESCE(?, status),
        start_date = COALESCE(?, start_date),
        expected_end_date = COALESCE(?, expected_end_date),
        cover_image = COALESCE(?, cover_image),
        brochure_url = COALESCE(?, brochure_url),
        architectural_plan_url = COALESCE(?, architectural_plan_url)
    WHERE id = ?
  `;
  
  await (db as any).query(query, [
    data.name !== undefined ? data.name : null,
    data.slug !== undefined ? data.slug : null,
    data.description !== undefined ? data.description : null,
    data.address !== undefined ? data.address : null,
    data.city !== undefined ? data.city : null,
    data.latitude !== undefined ? data.latitude : null,
    data.longitude !== undefined ? data.longitude : null,
    data.status !== undefined ? data.status : null,
    data.start_date !== undefined ? data.start_date : null,
    data.expected_end_date !== undefined ? data.expected_end_date : null,
    data.cover_image !== undefined ? data.cover_image : null,
    data.brochure_url !== undefined ? data.brochure_url : null,
    data.architectural_plan_url !== undefined ? data.architectural_plan_url : null,
    id
  ]);
};

export const remove = async (id: number, connection?: any): Promise<void> => {
  const db = connection || pool;
  await (db as any).query('DELETE FROM residence_projects WHERE id = ?', [id]);
};

export const getProjectMedia = async (projectId: number): Promise<any[]> => {
  const [rows] = await pool.query('SELECT * FROM residence_project_media WHERE residence_project_id = ? ORDER BY sort_order ASC, created_at DESC', [projectId]);
  return rows as any[];
};
