<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;
use App\Core\Pagination;

class PlayerController extends Controller
{
    /**
     * List players with search, filters, and pagination.
     */
    public function index(Request $request): Response
    {
        $db = App::getInstance()->db();

        $search = trim((string) $request->get('q', ''));
        $playerType = trim((string) $request->get('player_type', ''));
        $medicalStatus = trim((string) $request->get('medical_status', ''));
        $page = max(1, (int) $request->get('page', 1));
        $perPage = 25;

        $where = ['1=1'];
        $params = [];

        if ($search !== '') {
            $where[] = '(p.full_name_ar LIKE ? OR p.national_id LIKE ? OR p.registration_serial LIKE ?)';
            $params[] = "%{$search}%";
            $params[] = "%{$search}%";
            $params[] = "%{$search}%";
        }

        if ($playerType !== '') {
            $where[] = 'p.player_type = ?';
            $params[] = $playerType;
        }

        if ($medicalStatus !== '') {
            $where[] = 'p.medical_status = ?';
            $params[] = $medicalStatus;
        }

        $whereSql = implode(' AND ', $where);

        // Count total
        $countSql = "SELECT COUNT(*) as total FROM sa_players p WHERE {$whereSql}";
        $total = (int) $db->selectOne($countSql, $params)['total'];

        $pagination = Pagination::paginate($total, $perPage, $page);
        $offset = ($pagination['current_page'] - 1) * $perPage;

        // Fetch players
        $sql = "SELECT p.* FROM sa_players p WHERE {$whereSql} ORDER BY p.created_at DESC LIMIT {$perPage} OFFSET {$offset}";
        $players = $db->select($sql, $params);

        return $this->view('SportsActivity.Views.players.index', [
            'players'    => $players,
            'pagination' => $pagination,
            'filters'    => [
                'q'              => $search,
                'player_type'    => $playerType,
                'medical_status' => $medicalStatus,
            ],
        ]);
    }

    /**
     * Show the create player form.
     */
    public function create(Request $request): Response
    {
        return $this->view('SportsActivity.Views.players.create', []);
    }

    /**
     * Validate and store a new player.
     */
    public function store(Request $request): Response
    {
        $db = App::getInstance()->db();
        $session = App::getInstance()->session();

        $fullNameAr = trim((string) $request->post('full_name_ar', ''));
        $fullNameEn = trim((string) $request->post('full_name_en', ''));
        $playerType = trim((string) $request->post('player_type', ''));
        $memberId = (int) $request->post('member_id', 0);
        $nationalId = trim((string) $request->post('national_id', ''));
        $dateOfBirth = trim((string) $request->post('date_of_birth', ''));
        $gender = trim((string) $request->post('gender', 'male'));
        $phone = trim((string) $request->post('phone', ''));
        $email = trim((string) $request->post('email', ''));
        $guardianName = trim((string) $request->post('guardian_name', ''));
        $guardianPhone = trim((string) $request->post('guardian_phone', ''));
        $guardianNationalId = trim((string) $request->post('guardian_national_id', ''));
        $guardianRelationship = trim((string) $request->post('guardian_relationship', ''));
        $notes = trim((string) $request->post('notes', ''));

        // Validation
        $errors = [];
        if ($fullNameAr === '') {
            $errors[] = 'الاسم بالعربي مطلوب';
        }
        if ($playerType === '') {
            $errors[] = 'نوع اللاعب مطلوب';
        }
        if ($playerType === 'member' && $memberId <= 0) {
            $errors[] = 'رقم العضوية مطلوب للأعضاء';
        }

        $validTypes = ['member', 'non_member'];
        if (!in_array($playerType, $validTypes, true)) {
            $errors[] = 'نوع اللاعب غير صالح';
        }

        if (!empty($errors)) {
            $session->flash('_alerts', array_map(fn($e) => ['type' => 'error', 'message' => $e], $errors));
            $session->flash('_old_input', $request->all());
            return $this->redirect('/sa/players/create');
        }

        // Generate registration serial
        $lastSerial = $db->selectOne("SELECT registration_serial FROM sa_players ORDER BY id DESC LIMIT 1");
        $nextNum = 1;
        if ($lastSerial && !empty($lastSerial['registration_serial'])) {
            $parts = explode('-', $lastSerial['registration_serial']);
            $nextNum = (int) end($parts) + 1;
        }
        $serialNumber = 'SAP-' . str_pad((string) $nextNum, 5, '0', STR_PAD_LEFT);

        $data = [
            'registration_serial'  => $serialNumber,
            'full_name_ar'         => $fullNameAr,
            'full_name_en'         => $fullNameEn ?: null,
            'player_type'          => $playerType,
            'member_id'            => $memberId > 0 ? $memberId : null,
            'national_id'          => $nationalId ?: null,
            'date_of_birth'        => $dateOfBirth ?: null,
            'gender'               => $gender,
            'phone'                => $phone ?: null,
            'email'                => $email ?: null,
            'guardian_name'        => $guardianName ?: null,
            'guardian_phone'       => $guardianPhone ?: null,
            'guardian_national_id' => $guardianNationalId ?: null,
            'guardian_relationship' => $guardianRelationship ?: null,
            'medical_status'       => 'pending',
            'card_status'          => 'inactive',
            'notes'                => $notes ?: null,
            'created_at'           => now(),
            'updated_at'           => now(),
        ];

        $db->insert('sa_players', $data);
        $playerId = (int) $db->selectOne("SELECT LAST_INSERT_ID() as id")['id'];

        return $this->redirect('/sa/players/' . $playerId)->withSuccess('تم إضافة اللاعب بنجاح');
    }

    /**
     * Show player detail with documents list.
     */
    public function show(Request $request, string $id): Response
    {
        $db = App::getInstance()->db();

        $player = $db->selectOne("SELECT * FROM sa_players WHERE id = ?", [(int) $id]);
        if (!$player) {
            return $this->redirect('/sa/players')->withError('اللاعب غير موجود');
        }

        $documents = $db->select(
            "SELECT * FROM sa_player_documents WHERE player_id = ? ORDER BY created_at DESC",
            [(int) $id]
        );

        return $this->view('SportsActivity.Views.players.show', [
            'player'    => $player,
            'documents' => $documents,
        ]);
    }

    /**
     * Show edit form for a player.
     */
    public function edit(Request $request, string $id): Response
    {
        $db = App::getInstance()->db();

        $player = $db->selectOne("SELECT * FROM sa_players WHERE id = ?", [(int) $id]);
        if (!$player) {
            return $this->redirect('/sa/players')->withError('اللاعب غير موجود');
        }

        return $this->view('SportsActivity.Views.players.edit', [
            'player' => $player,
        ]);
    }

    /**
     * Validate and update a player.
     */
    public function update(Request $request, string $id): Response
    {
        $db = App::getInstance()->db();
        $session = App::getInstance()->session();

        $player = $db->selectOne("SELECT * FROM sa_players WHERE id = ?", [(int) $id]);
        if (!$player) {
            return $this->redirect('/sa/players')->withError('اللاعب غير موجود');
        }

        $fullNameAr = trim((string) $request->post('full_name_ar', ''));
        $fullNameEn = trim((string) $request->post('full_name_en', ''));
        $playerType = trim((string) $request->post('player_type', ''));
        $memberId = (int) $request->post('member_id', 0);
        $nationalId = trim((string) $request->post('national_id', ''));
        $dateOfBirth = trim((string) $request->post('date_of_birth', ''));
        $gender = trim((string) $request->post('gender', 'male'));
        $phone = trim((string) $request->post('phone', ''));
        $email = trim((string) $request->post('email', ''));
        $guardianName = trim((string) $request->post('guardian_name', ''));
        $guardianPhone = trim((string) $request->post('guardian_phone', ''));
        $guardianNationalId = trim((string) $request->post('guardian_national_id', ''));
        $guardianRelationship = trim((string) $request->post('guardian_relationship', ''));
        $notes = trim((string) $request->post('notes', ''));

        // Validation
        $errors = [];
        if ($fullNameAr === '') {
            $errors[] = 'الاسم بالعربي مطلوب';
        }
        if ($playerType === '') {
            $errors[] = 'نوع اللاعب مطلوب';
        }
        if ($playerType === 'member' && $memberId <= 0) {
            $errors[] = 'رقم العضوية مطلوب للأعضاء';
        }

        if (!empty($errors)) {
            $session->flash('_alerts', array_map(fn($e) => ['type' => 'error', 'message' => $e], $errors));
            $session->flash('_old_input', $request->all());
            return $this->redirect('/sa/players/' . $id . '/edit');
        }

        $data = [
            'full_name_ar'         => $fullNameAr,
            'full_name_en'         => $fullNameEn ?: null,
            'player_type'          => $playerType,
            'member_id'            => $memberId > 0 ? $memberId : null,
            'national_id'          => $nationalId ?: null,
            'date_of_birth'        => $dateOfBirth ?: null,
            'gender'               => $gender,
            'phone'                => $phone ?: null,
            'email'                => $email ?: null,
            'guardian_name'        => $guardianName ?: null,
            'guardian_phone'       => $guardianPhone ?: null,
            'guardian_national_id' => $guardianNationalId ?: null,
            'guardian_relationship' => $guardianRelationship ?: null,
            'notes'                => $notes ?: null,
            'updated_at'           => now(),
        ];

        $db->update('sa_players', $data, 'id = ?', [(int) $id]);

        return $this->redirect('/sa/players/' . $id)->withSuccess('تم تحديث بيانات اللاعب بنجاح');
    }
}
