<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class ConferenceWorkflowModel extends Model
{
    public function all(): array
    {
        return $this->db->query("
            SELECT cwr.*, c.title_ar, c.title_en
            FROM conference_workflow_rules cwr
            INNER JOIN conferences c ON c.id = cwr.conference_id
            ORDER BY c.id DESC
        ")->fetchAll();
    }

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

    public function save(int $conferenceId, array $data): void
    {
        $stmt = $this->db->prepare("
            INSERT INTO conference_workflow_rules (
                conference_id, min_reviewers, blind_review_mode,
                allow_author_edit_before_deadline, allow_revision_round, require_camera_ready,
                auto_ack_email, auto_reviewer_assignment_email, auto_decision_email,
                auto_revision_email, auto_camera_ready_email
            ) VALUES (
                :conference_id, :min_reviewers, :blind_review_mode,
                :allow_author_edit_before_deadline, :allow_revision_round, :require_camera_ready,
                :auto_ack_email, :auto_reviewer_assignment_email, :auto_decision_email,
                :auto_revision_email, :auto_camera_ready_email
            )
            ON DUPLICATE KEY UPDATE
                min_reviewers = VALUES(min_reviewers),
                blind_review_mode = VALUES(blind_review_mode),
                allow_author_edit_before_deadline = VALUES(allow_author_edit_before_deadline),
                allow_revision_round = VALUES(allow_revision_round),
                require_camera_ready = VALUES(require_camera_ready),
                auto_ack_email = VALUES(auto_ack_email),
                auto_reviewer_assignment_email = VALUES(auto_reviewer_assignment_email),
                auto_decision_email = VALUES(auto_decision_email),
                auto_revision_email = VALUES(auto_revision_email),
                auto_camera_ready_email = VALUES(auto_camera_ready_email)
        ");
        $stmt->execute([
            ':conference_id' => $conferenceId,
            ':min_reviewers' => (int) ($data['min_reviewers'] ?? 2),
            ':blind_review_mode' => $data['blind_review_mode'] ?? 'double',
            ':allow_author_edit_before_deadline' => !empty($data['allow_author_edit_before_deadline']) ? 1 : 0,
            ':allow_revision_round' => !empty($data['allow_revision_round']) ? 1 : 0,
            ':require_camera_ready' => !empty($data['require_camera_ready']) ? 1 : 0,
            ':auto_ack_email' => !empty($data['auto_ack_email']) ? 1 : 0,
            ':auto_reviewer_assignment_email' => !empty($data['auto_reviewer_assignment_email']) ? 1 : 0,
            ':auto_decision_email' => !empty($data['auto_decision_email']) ? 1 : 0,
            ':auto_revision_email' => !empty($data['auto_revision_email']) ? 1 : 0,
            ':auto_camera_ready_email' => !empty($data['auto_camera_ready_email']) ? 1 : 0,
        ]);
    }
}
