<?hh // strict

namespace App\Services;

use App\Models\Application;
use App\Models\Tenant;
use PDO;

class ApplicationService
{
    private PDO $db;
    
    public function __construct(PDO $db)
    {
        $this->db = $db;
    }
    
    /**
     * Submit a new application for a job
     */
    public function submitApplication(Application $application): bool
    {
        $sql = "INSERT INTO applications (job_id, user_id, resume_url, cover_letter, status, tenant_id) VALUES (:job_id, :user_id, :resume_url, :cover_letter, :status, :tenant_id)";
        $stmt = $this->db->prepare($sql);
        
        return $stmt->execute([
            ':job_id' => $application->getJobId(),
            ':user_id' => $application->getUserId(),
            ':resume_url' => $application->getResumeUrl(),
            ':cover_letter' => $application->getCoverLetter(),
            ':status' => $application->getStatus(),
            ':tenant_id' => $application->getTenantId()
        ]);
    }
    
    /**
     * Get applications for a specific user (job seeker)
     */
    public function getApplicationsByUser(int $userId, ?Tenant $tenant): array
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "SELECT * FROM applications WHERE user_id = :user_id AND tenant_id = :tenant_id ORDER BY created_at DESC";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':user_id' => $userId,
            ':tenant_id' => $tenantId
        ]);
        
        $applications = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $applications[] = new Application(
                id: (int)$row['id'],
                jobId: (int)$row['job_id'],
                userId: (int)$row['user_id'],
                resumeUrl: $row['resume_url'],
                coverLetter: $row['cover_letter'],
                status: $row['status'],
                tenantId: $row['tenant_id']
            );
        }
        
        return $applications;
    }
    
    /**
     * Get applications for a specific job (for job provider)
     */
    public function getApplicationsByJob(int $jobId, ?Tenant $tenant): array
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "SELECT * FROM applications WHERE job_id = :job_id AND tenant_id = :tenant_id ORDER BY created_at DESC";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':job_id' => $jobId,
            ':tenant_id' => $tenantId
        ]);
        
        $applications = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $applications[] = new Application(
                id: (int)$row['id'],
                jobId: (int)$row['job_id'],
                userId: (int)$row['user_id'],
                resumeUrl: $row['resume_url'],
                coverLetter: $row['cover_letter'],
                status: $row['status'],
                tenantId: $row['tenant_id']
            );
        }
        
        return $applications;
    }
    
    /**
     * Update application status
     */
    public function updateApplicationStatus(int $applicationId, string $newStatus, ?Tenant $tenant): bool
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "UPDATE applications SET status = :status, updated_at = NOW() WHERE id = :id AND tenant_id = :tenant_id";
        $stmt = $this->db->prepare($sql);
        
        $result = $stmt->execute([
            ':id' => $applicationId,
            ':status' => $newStatus,
            ':tenant_id' => $tenantId
        ]);
        
        return $result && $stmt->rowCount() > 0;
    }
}