<?php
declare(strict_types=1);

namespace App\Controllers;

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

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

use App\Core\Controller;
use App\Models\Track;
use App\Models\Conference;
use App\Models\User;

final class TrackController extends Controller
{
    public function index(): void
    {
        require_permission('tracks.manage');

        $conferenceId = (int)
\request_conference_id();
        if ($conferenceId <= 0) {
            redirect('admin/conferences');
        }

        $conferenceModel = new Conference();
        $trackModel = new Track();

        $conference = $conferenceModel->findById($conferenceId);
        if (!$conference) {
            redirect('admin/conferences');
        }

        $this->view('admin/tracks/index', [
            'conference' => $conference,
            'tracks' => $trackModel->allByConference($conferenceId),
        ]);
    }

    public function create(): void
    {
        require_permission('tracks.create');

        $conferenceId = (int)
\request_conference_id();
        if ($conferenceId <= 0) {
            redirect('admin/conferences');
        }

        $conferenceModel = new Conference();
        $trackModel = new Track();
        $userModel = new User();

        $conference = $conferenceModel->findById($conferenceId);
        if (!$conference) {
            redirect('admin/conferences');
        }

        if ($this->isPost()) {
            csrf_check();

            $data = $this->collectTrackForm();
            $data['conference_id'] = $conferenceId;

            if ($data['name_ar'] === '' || $data['name_en'] === '') {
                flash('danger', trans('required_fields'));
                redirect('admin/tracks/create?conference_id=' . $conferenceId);
            }

            $newId = $trackModel->create($data);
            audit_log('create', 'tracks', $newId, ['conference_id' => $conferenceId]);

            flash('success', trans('track_created_success'));
            redirect('admin/tracks?conference_id=' . $conferenceId);
        }

        $this->view('admin/tracks/create', [
            'conference' => $conference,
            'chairs' => $userModel->trackChairs(),
        ]);
    }

    public function edit(): void
    {
        require_permission('tracks.edit');

        $id = (int) ($_GET['id'] ?? 0);
        if ($id <= 0) {
            redirect('admin/conferences');
        }

        $trackModel = new Track();
        $conferenceModel = new Conference();
        $userModel = new User();

        $track = $trackModel->findById($id);
        if (!$track) {
            redirect('admin/conferences');
        }

        $conference = $conferenceModel->findById((int) $track['conference_id']);

        if ($this->isPost()) {
            csrf_check();

            $data = $this->collectTrackForm();
            $data['conference_id'] = (int) $track['conference_id'];

            if ($data['name_ar'] === '' || $data['name_en'] === '') {
                flash('danger', trans('required_fields'));
                redirect('admin/tracks/edit?id=' . $id);
            }

            $trackModel->update($id, $data);
            audit_log('update', 'tracks', $id, ['conference_id' => $track['conference_id']]);

            flash('success', trans('track_updated_success'));
            redirect('admin/tracks?conference_id=' . (int) $track['conference_id']);
        }

        $this->view('admin/tracks/edit', [
            'track' => $track,
            'conference' => $conference,
            'chairs' => $userModel->trackChairs(),
        ]);
    }

    private function collectTrackForm(): array
    {
        return [
            'chair_user_id' => (int) ($_POST['chair_user_id'] ?? 0),
            'name_ar' => trim((string) ($_POST['name_ar'] ?? '')),
            'name_en' => trim((string) ($_POST['name_en'] ?? '')),
            'description_ar' => trim((string) ($_POST['description_ar'] ?? '')),
            'description_en' => trim((string) ($_POST['description_en'] ?? '')),
            'max_reviewers' => (int) ($_POST['max_reviewers'] ?? 3),
            'min_reviewers' => (int) ($_POST['min_reviewers'] ?? 2),
            'is_active' => isset($_POST['is_active']) ? 1 : 0,
        ];
    }
}
