<?php
// src/Models/ApplicationModel.php
namespace App\Models;

use App\Database\DatabaseManager;
use PDO;

class ApplicationModel
{
    private $db;

    public function __construct()
    {
        $this->db = DatabaseManager::connect();
    }

    public function findById(string $id): ?array
    {
        $stmt = $this->db->prepare('SELECT * FROM applications WHERE id = :id');
        $stmt->bindParam(':id', $id);
        $stmt->execute();

        return $stmt->fetch() ?: null;
    }

    public function findByJobPosition(string $jobPositionId, string $tenantId): array
    {
        $stmt = $this->db->prepare('
            SELECT a.*, u.first_name, u.last_name, u.email 
            FROM applications a
            JOIN users u ON a.applicant_id = u.id
            JOIN job_positions jp ON a.job_position_id = jp.id
            WHERE a.job_position_id = :job_position_id AND jp.tenant_id = :tenant_id
            ORDER BY a.created_at DESC
        ');
        $stmt->bindParam(':job_position_id', $jobPositionId);
        $stmt->bindParam(':tenant_id', $tenantId);
        $stmt->execute();

        return $stmt->fetchAll();
    }

    public function findByApplicant(string $applicantId, string $tenantId): array
    {
        $stmt = $this->db->prepare('
            SELECT a.*, jp.title as position_title 
            FROM applications a
            JOIN job_positions jp ON a.job_position_id = jp.id
            WHERE a.applicant_id = :applicant_id AND jp.tenant_id = :tenant_id
            ORDER BY a.created_at DESC
        ');
        $stmt->bindParam(':applicant_id', $applicantId);
        $stmt->bindParam(':tenant_id', $tenantId);
        $stmt->execute();

        return $stmt->fetchAll();
    }

    public function create(array $data): string
    {
        $id = bin2hex(random_bytes(16)); // Simple ID generation for demo
        
        $stmt = $this->db->prepare('
            INSERT INTO applications (id, job_position_id, applicant_id, resume_path, cover_letter, status) 
            VALUES (:id, :job_position_id, :applicant_id, :resume_path, :cover_letter, :status)
            RETURNING id
        ');
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':job_position_id', $data['job_position_id']);
        $stmt->bindParam(':applicant_id', $data['applicant_id']);
        $stmt->bindParam(':resume_path', $data['resume_path']);
        $stmt->bindParam(':cover_letter', $data['cover_letter']);
        $stmt->bindParam(':status', $data['status']);
        
        $stmt->execute();
        $result = $stmt->fetch();
        
        return $result['id'];
    }
    
    public function updateStatus(string $id, string $status, string $tenantId): bool
    {
        $stmt = $this->db->prepare('
            UPDATE applications a
            SET status = :status, updated_at = CURRENT_TIMESTAMP
            FROM job_positions jp
            WHERE a.id = :id AND a.job_position_id = jp.id AND jp.tenant_id = :tenant_id
        ');
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':status', $status);
        $stmt->bindParam(':tenant_id', $tenantId);
        
        return $stmt->execute();
    }
}