<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class ReviewCriteriaModel extends Model
{
    public function byConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM review_criteria
            WHERE conference_id = :conference_id
            ORDER BY sort_order ASC, id ASC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }

    public function activeByConference(int $conferenceId): array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM review_criteria
            WHERE conference_id = :conference_id
              AND is_active = 1
            ORDER BY sort_order ASC, id ASC
        ");
        $stmt->execute([':conference_id' => $conferenceId]);
        return $stmt->fetchAll();
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("
            SELECT *
            FROM review_criteria
            WHERE id = :id
            LIMIT 1
        ");
        $stmt->execute([':id' => $id]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    public function create(array $data): int
    {
        $stmt = $this->db->prepare("
            INSERT INTO review_criteria (
                conference_id, criterion_key, title_en, title_ar, description_en, description_ar,
                min_score, max_score, weight, sort_order, is_active
            ) VALUES (
                :conference_id, :criterion_key, :title_en, :title_ar, :description_en, :description_ar,
                :min_score, :max_score, :weight, :sort_order, :is_active
            )
        ");
        $stmt->execute([
            ':conference_id' => $data['conference_id'],
            ':criterion_key' => $data['criterion_key'],
            ':title_en' => $data['title_en'],
            ':title_ar' => $data['title_ar'],
            ':description_en' => $data['description_en'] ?: null,
            ':description_ar' => $data['description_ar'] ?: null,
            ':min_score' => $data['min_score'],
            ':max_score' => $data['max_score'],
            ':weight' => $data['weight'],
            ':sort_order' => $data['sort_order'] ?? 0,
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
        ]);
        return (int) $this->db->lastInsertId();
    }

    public function update(int $id, array $data): void
    {
        $stmt = $this->db->prepare("
            UPDATE review_criteria
            SET conference_id = :conference_id,
                criterion_key = :criterion_key,
                title_en = :title_en,
                title_ar = :title_ar,
                description_en = :description_en,
                description_ar = :description_ar,
                min_score = :min_score,
                max_score = :max_score,
                weight = :weight,
                sort_order = :sort_order,
                is_active = :is_active
            WHERE id = :id
        ");
        $stmt->execute([
            ':id' => $id,
            ':conference_id' => $data['conference_id'],
            ':criterion_key' => $data['criterion_key'],
            ':title_en' => $data['title_en'],
            ':title_ar' => $data['title_ar'],
            ':description_en' => $data['description_en'] ?: null,
            ':description_ar' => $data['description_ar'] ?: null,
            ':min_score' => $data['min_score'],
            ':max_score' => $data['max_score'],
            ':weight' => $data['weight'],
            ':sort_order' => $data['sort_order'] ?? 0,
            ':is_active' => !empty($data['is_active']) ? 1 : 0,
        ]);
    }

    public function saveScores(int $reviewId, array $criteria, array $input): void
    {
        $stmt = $this->db->prepare("
            INSERT INTO review_criterion_scores (
                review_id, criterion_id, score
            ) VALUES (
                :review_id, :criterion_id, :score
            )
            ON DUPLICATE KEY UPDATE score = VALUES(score)
        ");

        foreach ($criteria as $criterion) {
            $cid = (int) $criterion['id'];
            $score = isset($input[$cid]) ? (float) $input[$cid] : 0;

            $stmt->execute([
                ':review_id' => $reviewId,
                ':criterion_id' => $cid,
                ':score' => $score,
            ]);
        }
    }

    public function scoresByReview(int $reviewId): array
    {
        $stmt = $this->db->prepare("
            SELECT criterion_id, score
            FROM review_criterion_scores
            WHERE review_id = :review_id
        ");
        $stmt->execute([':review_id' => $reviewId]);

        $out = [];
        foreach ($stmt->fetchAll() as $row) {
            $out[(int) $row['criterion_id']] = (float) $row['score'];
        }
        return $out;
    }
}
