<?php

class SeasonController extends Controller
{
    public function index(Request $request): void
    {
        $sports = $this->db()->fetchAll("SELECT id, name_en FROM sports WHERE is_active = 1 ORDER BY name_en");
        $this->view('seasons/index', [
            'title' => lang('sports.seasons'),
            'breadcrumbs' => [['label' => lang('sports.title'), 'url' => '/sports'], ['label' => lang('sports.seasons')]],
            'sports' => $sports,
        ]);
    }

    public function create(Request $request): void
    {
        $sports = $this->db()->fetchAll("SELECT id, name_en FROM sports WHERE is_active = 1 ORDER BY name_en");
        $this->view('seasons/create', [
            'title' => lang('sports.create_season'),
            'breadcrumbs' => [['label' => lang('sports.title'), 'url' => '/sports'], ['label' => lang('sports.seasons'), 'url' => '/sports/seasons'], ['label' => lang('common.create')]],
            'sports' => $sports,
        ]);
    }

    public function store(Request $request): void
    {
        if (!CSRF::validate($request->csrfToken())) { $this->json(['success' => false, 'message' => 'Invalid token'], 403); return; }
        $validator = new Validator($request->all(), [
            'season_code' => 'required|string|max:20|unique:sport_seasons,season_code',
            'sport_id' => 'required|integer|exists:sports,id',
            'name_en' => 'required|string|max:200',
            'name_ar' => 'required|string|max:200',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'registration_start' => 'nullable|date',
            'registration_end' => 'nullable|date',
            'status' => 'required|in:upcoming,registration,active,completed,cancelled',
            'max_participants' => 'nullable|integer',
            'fee_amount' => 'nullable|decimal',
        ]);
        if ($validator->fails()) { $this->json(['success' => false, 'errors' => $validator->errors()], 422); return; }
        $data = $validator->validated();
        $data['fee_amount'] = $data['fee_amount'] ?? 0;
        $id = $this->db()->insert('sport_seasons', $data);
        $this->audit('create', 'sport_season', $id, "Created season: {$data['name_en']}");
        $this->json(['success' => true, 'message' => 'Season created', 'redirect' => '/sports/seasons']);
    }

    public function edit(Request $request): void
    {
        $id = (int) $request->routeParam('id');
        $season = $this->db()->fetchOne("SELECT * FROM sport_seasons WHERE id = ?", [$id]);
        if (!$season) { $this->abort(404); return; }
        $sports = $this->db()->fetchAll("SELECT id, name_en FROM sports WHERE is_active = 1 ORDER BY name_en");
        $this->view('seasons/edit', [
            'title' => 'Edit: ' . $season['name_en'],
            'breadcrumbs' => [['label' => lang('sports.title'), 'url' => '/sports'], ['label' => lang('sports.seasons'), 'url' => '/sports/seasons'], ['label' => lang('common.edit')]],
            'season' => $season, 'sports' => $sports,
        ]);
    }

    public function update(Request $request): void
    {
        $id = (int) $request->routeParam('id');
        if (!CSRF::validate($request->csrfToken())) { $this->json(['success' => false, 'message' => 'Invalid token'], 403); return; }
        $validator = new Validator($request->all(), [
            'name_en' => 'required|string|max:200',
            'name_ar' => 'required|string|max:200',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'registration_start' => 'nullable|date',
            'registration_end' => 'nullable|date',
            'status' => 'required|in:upcoming,registration,active,completed,cancelled',
            'max_participants' => 'nullable|integer',
            'fee_amount' => 'nullable|decimal',
        ]);
        if ($validator->fails()) { $this->json(['success' => false, 'errors' => $validator->errors()], 422); return; }
        $data = $validator->validated();
        $this->db()->update('sport_seasons', $data, 'id = ?', [$id]);
        $this->audit('update', 'sport_season', $id, "Updated season: {$data['name_en']}");
        $this->json(['success' => true, 'message' => 'Season updated', 'redirect' => '/sports/seasons']);
    }
}