<?php

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use App\Core\Controller;
use App\Core\Database;
use App\Core\TenantContext;

class ConferenceThemesController extends Controller
{
    public function index()
    {
        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

        if ($conferenceId <= 0) {

            $conferenceId =
                (int)($_SESSION['conference_id'] ?? 0);
        }

        $themes = [];

        try {

            $stmt = $db->prepare("
                SELECT *
                FROM tracks
                WHERE conference_id = ?
                ORDER BY
                    is_active DESC,
                    sort_order ASC,
                    id ASC
            ");

            $stmt->execute([
                $conferenceId
            ]);

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

        } catch (\Throwable $e) {

            error_log(
                'TRACKS LOAD ERROR: '
                . $e->getMessage()
            );

            $themes = [];
        }

        $this->render(
            'admin/conference_themes/index',
            [
                'themes' => $themes,
                'conferenceId' => $conferenceId
            ]
        );
    }

    public function create()
    {
        $this->render(
            'admin/conference_themes/create'
        );
    }

    public function store()
    {
        $db = Database::getInstance();

        $conferenceId =
            TenantContext::conferenceId();

        $orgId =
            TenantContext::orgId();

        $stmt = $db->prepare("
            INSERT INTO tracks
            (
                conference_id,
                org_id,
                name_en,
                name_ar,
                description_en,
                description_ar,
                max_reviewers,
                min_reviewers,
                is_active
            )
            VALUES
            (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");

        $stmt->execute([

            $conferenceId,
            $orgId,

            trim($_POST['name_en'] ?? ''),
            trim($_POST['name_ar'] ?? ''),

            trim($_POST['description_en'] ?? ''),
            trim($_POST['description_ar'] ?? ''),

            (int)($_POST['max_reviewers'] ?? 3),
            (int)($_POST['min_reviewers'] ?? 2),

            (int)($_POST['is_active'] ?? 1)

        ]);

        $_SESSION['success'] =
            'Conference theme created successfully';

        header(
            "Location: /easychair/admin/conference-themes"
        );

        exit;
    }

    public function edit()
    {
        $db = Database::getInstance();

        $id =
            (int)($_GET['id'] ?? 0);

        $stmt = $db->prepare("
            SELECT *
            FROM tracks
            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([$id]);

        $theme =
            $stmt->fetch(\PDO::FETCH_ASSOC);

        if (!$theme) {

            die('Theme not found.');
        }

        $this->render(
            'admin/conference_themes/edit',
            [
                'theme' => $theme
            ]
        );
    }

    public function update()
    {
        $db = Database::getInstance();

        $id =
            (int)($_POST['id'] ?? 0);

        $stmt = $db->prepare("
            UPDATE tracks
            SET

                name_en = ?,
                name_ar = ?,

                description_en = ?,
                description_ar = ?,

                max_reviewers = ?,
                min_reviewers = ?,

                is_active = ?

            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([

            trim($_POST['name_en'] ?? ''),
            trim($_POST['name_ar'] ?? ''),

            trim($_POST['description_en'] ?? ''),
            trim($_POST['description_ar'] ?? ''),

            (int)($_POST['max_reviewers'] ?? 3),
            (int)($_POST['min_reviewers'] ?? 2),

            (int)($_POST['is_active'] ?? 1),

            $id

        ]);

        $_SESSION['success'] =
            'Theme updated successfully';

        header(
            "Location: /easychair/admin/conference-themes"
        );

        exit;
    }

    public function delete()
    {
        $db = Database::getInstance();

        $id =
            (int)($_GET['id'] ?? 0);

        $stmt = $db->prepare("
            DELETE FROM tracks
            WHERE id = ?
            LIMIT 1
        ");

        $stmt->execute([$id]);

        $_SESSION['success'] =
            'Theme deleted successfully';

        header(
            "Location: /easychair/admin/conference-themes"
        );

        exit;
    }
}
