<?php
// backend/models/Resume.php
require_once __DIR__ . '/../config/database.php';

class Resume {
    private $conn;
    private $table = "resumes";

    public function __construct() {
        $database = new Database();
        $this->conn = $database->getConnection();
    }

    public function save($userId, $resumeData, $template, $atsScore, $jobDescription) {
        $query = "INSERT INTO " . $this->table . " (user_id, resume_data, template, ats_score, job_description) 
                  VALUES (:user_id, :resume_data, :template, :ats_score, :job_description)";
        $stmt = $this->conn->prepare($query);
        $resumeDataJson = json_encode($resumeData);
        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':resume_data', $resumeDataJson);
        $stmt->bindParam(':template', $template);
        $stmt->bindParam(':ats_score', $atsScore);
        $stmt->bindParam(':job_description', $jobDescription);
        return $stmt->execute();
    }

    public function update($id, $userId, $resumeData, $template, $atsScore, $jobDescription) {
        $query = "UPDATE " . $this->table . " 
                  SET resume_data = :resume_data, template = :template, ats_score = :ats_score, job_description = :job_description, updated_at = NOW()
                  WHERE id = :id AND user_id = :user_id";
        $stmt = $this->conn->prepare($query);
        $resumeDataJson = json_encode($resumeData);
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':resume_data', $resumeDataJson);
        $stmt->bindParam(':template', $template);
        $stmt->bindParam(':ats_score', $atsScore);
        $stmt->bindParam(':job_description', $jobDescription);
        return $stmt->execute();
    }

    public function findByUser($userId) {
        $query = "SELECT * FROM " . $this->table . " WHERE user_id = :user_id ORDER BY updated_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':user_id', $userId);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function findById($id, $userId) {
        $query = "SELECT * FROM " . $this->table . " WHERE id = :id AND user_id = :user_id LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':user_id', $userId);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($result) {
            $result['resume_data'] = json_decode($result['resume_data'], true);
        }
        return $result;
    }

    public function getLastResume($userId) {
        $query = "SELECT * FROM " . $this->table . " WHERE user_id = :user_id ORDER BY updated_at DESC LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':user_id', $userId);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($result) {
            $result['resume_data'] = json_decode($result['resume_data'], true);
        }
        return $result;
    }
}
?>