<?php
// app/Models/Engagement.php

class Engagement extends Database {
    private function getSessionId() {
        return session_id();
    }

    // ---------- Favorites ----------
    public function toggleFavorite($property_id) {
        $session_id = $this->getSessionId();
        if ($this->isFavorite($property_id)) {
            $this->query("DELETE FROM property_favorites WHERE property_id = :property_id AND session_id = :session_id");
            $this->bind(':property_id', $property_id);
            $this->bind(':session_id', $session_id);
            return $this->execute();
        }

        $this->query("INSERT INTO property_favorites (property_id, session_id) VALUES (:property_id, :session_id)");
        $this->bind(':property_id', $property_id);
        $this->bind(':session_id', $session_id);
        return $this->execute();
    }

    public function isFavorite($property_id) {
        $session_id = $this->getSessionId();
        $this->query("SELECT id FROM property_favorites WHERE property_id = :property_id AND session_id = :session_id LIMIT 1");
        $this->bind(':property_id', $property_id);
        $this->bind(':session_id', $session_id);
        return (bool) $this->single();
    }

    public function getFavorites() {
        $session_id = $this->getSessionId();
        $this->query("
            SELECT p.* 
            FROM property_favorites f
            JOIN properties p ON p.id = f.property_id
            WHERE f.session_id = :session_id
            ORDER BY f.created_at DESC
        ");
        $this->bind(':session_id', $session_id);
        return $this->resultSet();
    }

    public function getFavoriteIds() {
        $session_id = $this->getSessionId();
        $this->query("SELECT property_id FROM property_favorites WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        $rows = $this->resultSet();
        return array_map(fn($r) => (int) $r->property_id, $rows ?: []);
    }

    public function countFavorites() {
        $session_id = $this->getSessionId();
        $this->query("SELECT COUNT(*) as count FROM property_favorites WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        $row = $this->single();
        return $row ? (int) $row->count : 0;
    }

    public function clearFavorites() {
        $session_id = $this->getSessionId();
        $this->query("DELETE FROM property_favorites WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        return $this->execute();
    }

    // ---------- Compare ----------
    public function toggleCompare($property_id) {
        $session_id = $this->getSessionId();
        if ($this->isCompared($property_id)) {
            $this->query("DELETE FROM property_compares WHERE property_id = :property_id AND session_id = :session_id");
            $this->bind(':property_id', $property_id);
            $this->bind(':session_id', $session_id);
            return $this->execute();
        }

        $this->query("INSERT INTO property_compares (property_id, session_id) VALUES (:property_id, :session_id)");
        $this->bind(':property_id', $property_id);
        $this->bind(':session_id', $session_id);
        return $this->execute();
    }

    public function isCompared($property_id) {
        $session_id = $this->getSessionId();
        $this->query("SELECT id FROM property_compares WHERE property_id = :property_id AND session_id = :session_id LIMIT 1");
        $this->bind(':property_id', $property_id);
        $this->bind(':session_id', $session_id);
        return (bool) $this->single();
    }

    public function getCompares() {
        $session_id = $this->getSessionId();
        $this->query("
            SELECT p.* 
            FROM property_compares c
            JOIN properties p ON p.id = c.property_id
            WHERE c.session_id = :session_id
            ORDER BY c.created_at DESC
        ");
        $this->bind(':session_id', $session_id);
        return $this->resultSet();
    }

    public function getCompareIds() {
        $session_id = $this->getSessionId();
        $this->query("SELECT property_id FROM property_compares WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        $rows = $this->resultSet();
        return array_map(fn($r) => (int) $r->property_id, $rows ?: []);
    }

    public function countCompares() {
        $session_id = $this->getSessionId();
        $this->query("SELECT COUNT(*) as count FROM property_compares WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        $row = $this->single();
        return $row ? (int) $row->count : 0;
    }

    public function clearCompares() {
        $session_id = $this->getSessionId();
        $this->query("DELETE FROM property_compares WHERE session_id = :session_id");
        $this->bind(':session_id', $session_id);
        return $this->execute();
    }
}
