<?hh // strict

namespace App\Services;

use App\Models\Job;
use App\Models\Tenant;
use PDO;

class JobService
{
    private PDO $db;
    
    public function __construct(PDO $db)
    {
        $this->db = $db;
    }
    
    /**
     * Get all jobs for the current tenant
     */
    public function getAllJobs(?Tenant $tenant, ?array $filters = null): array
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "SELECT * FROM jobs WHERE tenant_id = :tenant_id AND status = 'active'";
        
        // Add filters if provided
        $params = [':tenant_id' => $tenantId];
        
        if ($filters) {
            if (isset($filters['location'])) {
                $sql .= " AND location LIKE :location";
                $params[':location'] = '%' . $filters['location'] . '%';
            }
            
            if (isset($filters['keywords'])) {
                $sql .= " AND (title LIKE :keywords OR description LIKE :keywords)";
                $params[':keywords'] = '%' . $filters['keywords'] . '%';
            }
        }
        
        $sql .= " ORDER BY created_at DESC";
        
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        
        $jobs = [];
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $jobs[] = new Job(
                id: (int)$row['id'],
                title: $row['title'],
                description: $row['description'],
                location: $row['location'],
                employmentType: $row['employment_type'],
                tenantId: $row['tenant_id']
            );
        }
        
        return $jobs;
    }
    
    /**
     * Get a specific job by ID for the current tenant
     */
    public function getJobById(int $jobId, ?Tenant $tenant): ?Job
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "SELECT * FROM jobs WHERE id = :id AND tenant_id = :tenant_id";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':id' => $jobId,
            ':tenant_id' => $tenantId
        ]);
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$row) {
            return null;
        }
        
        return new Job(
            id: (int)$row['id'],
            title: $row['title'],
            description: $row['description'],
            location: $row['location'],
            employmentType: $row['employment_type'],
            tenantId: $row['tenant_id']
        );
    }
    
    /**
     * Create a new job posting for the tenant
     */
    public function createJob(Job $job): bool
    {
        $sql = "INSERT INTO jobs (title, description, location, employment_type, tenant_id) VALUES (:title, :description, :location, :employment_type, :tenant_id)";
        $stmt = $this->db->prepare($sql);
        
        return $stmt->execute([
            ':title' => $job->getTitle(),
            ':description' => $job->getDescription(),
            ':location' => $job->getLocation(),
            ':employment_type' => $job->getEmploymentType(),
            ':tenant_id' => $job->getTenantId()
        ]);
    }
    
    /**
     * Update an existing job for the tenant
     */
    public function updateJob(Job $job): bool
    {
        $sql = "UPDATE jobs SET title = :title, description = :description, location = :location, employment_type = :employment_type, updated_at = NOW() WHERE id = :id AND tenant_id = :tenant_id";
        $stmt = $this->db->prepare($sql);
        
        return $stmt->execute([
            ':id' => $job->getId(),
            ':title' => $job->getTitle(),
            ':description' => $job->getDescription(),
            ':location' => $job->getLocation(),
            ':employment_type' => $job->getEmploymentType(),
            ':tenant_id' => $job->getTenantId()
        ]);
    }
    
    /**
     * Delete a job for the tenant
     */
    public function deleteJob(int $jobId, ?Tenant $tenant): bool
    {
        $tenantId = $tenant ? $tenant->getId() : 'default';
        
        $sql = "DELETE FROM jobs WHERE id = :id AND tenant_id = :tenant_id";
        $stmt = $this->db->prepare($sql);
        
        $result = $stmt->execute([
            ':id' => $jobId,
            ':tenant_id' => $tenantId
        ]);
        
        return $result && $stmt->rowCount() > 0;
    }
}