<?php
class ProjectPhoto extends Model
{
    public function all(): array
    {
        return $this->fetchAll('SELECT pp.*, p.name AS project_name FROM project_photos pp LEFT JOIN projects p ON pp.project_id = p.id ORDER BY pp.uploaded_at DESC');
    }

    public function create(array $data): bool
    {
        return $this->execute('INSERT INTO project_photos (project_id, photo_type, file_path, caption, uploaded_at) VALUES (:project_id, :photo_type, :file_path, :caption, NOW())', [
            'project_id' => $data['project_id'],
            'photo_type' => $data['photo_type'],
            'file_path' => $data['file_path'],
            'caption' => $data['caption'] ?? null,
        ]);
    }

    public function count(): int
    {
        $result = $this->fetch('SELECT COUNT(*) AS total FROM project_photos');
        return (int)($result['total'] ?? 0);
    }

    public function findById(int $id): ?array
    {
        return $this->fetch('SELECT pp.*, p.name AS project_name FROM project_photos pp LEFT JOIN projects p ON pp.project_id = p.id WHERE pp.id = :id LIMIT 1', ['id' => $id]);
    }

    public function delete(int $id): bool
    {
        return $this->execute('DELETE FROM project_photos WHERE id = :id', ['id' => $id]);
    }
}
