<?php

namespace Core\App\Models;

use PDO;

class ProductReviewModel
{
    protected PDO $db;

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

    /* =====================================================
     * Get approved reviews by product (FRONTEND)
     * ===================================================== */
    public function getApprovedByProduct(int $productId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM product_reviews
            WHERE product_id = ?
              AND status = 'approved'
            ORDER BY created_at DESC
        ");

        $stmt->execute([$productId]);
        $reviews = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if (!$reviews) {
            return [];
        }

        foreach ($reviews as &$review) {
            $review['images'] = $this->getImagesByReview((int)$review['id']);
        }

        return $reviews;
    }

    /* =====================================================
     * Get images for a review
     * ===================================================== */
    public function getImagesByReview(int $reviewId): array
    {
        $stmt = $this->db->prepare("
            SELECT image_path
            FROM product_review_images
            WHERE review_id = ?
            ORDER BY id ASC
        ");

        $stmt->execute([$reviewId]);
        $images = $stmt->fetchAll(PDO::FETCH_COLUMN) ?: [];

foreach ($images as &$img) {
    if ($img && strpos($img, '/') !== 0) {
        $img = '/' . $img;
    }
}

return $images;

    }

    /* =====================================================
     * Create review (pending)
     * ===================================================== */
    public function create(array $data): int
    {
        $stmt = $this->db->prepare("
            INSERT INTO product_reviews
            (product_id, name, email, rating, comment, status, created_at)
            VALUES (?, ?, ?, ?, ?, 'pending', NOW())
        ");

        $stmt->execute([
            $data['product_id'],
            $data['name'],
            $data['email'],
            $data['rating'],
            $data['comment'],
        ]);

        return (int)$this->db->lastInsertId();
    }

    /* =====================================================
     * Attach image to review
     * ===================================================== */
    public function addImage(int $reviewId, string $path): void
    {
        $stmt = $this->db->prepare("
            INSERT INTO product_review_images
            (review_id, image_path, created_at)
            VALUES (?, ?, NOW())
        ");

        $stmt->execute([
            $reviewId,
            $path
        ]);
    }

    /* =====================================================
     * Get all reviews (ADMIN)
     * ===================================================== */
    public function getAll(): array
    {
        $stmt = $this->db->query("
            SELECT r.*, p.title AS product_title
            FROM product_reviews r
            LEFT JOIN products p ON p.id = r.product_id
            ORDER BY r.created_at DESC
        ");

        $reviews = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if (!$reviews) {
            return [];
        }

        foreach ($reviews as &$review) {
            $review['images'] = $this->getImagesByReview((int)$review['id']);
        }

        return $reviews;
    }
}
