<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers\Api;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Modules\Members\Services\MembershipValidationService;
use App\Modules\Members\Services\NationalIdParser;

class MemberLookupApiController extends Controller
{
    public function lookup(Request $request): Response
    {
        $membershipNumber = trim((string) $request->post('membership_number', ''));
        $nationalId = trim((string) $request->post('national_id', ''));

        if ($membershipNumber === '' && $nationalId === '') {
            return $this->json(['success' => false, 'error' => 'أدخل رقم العضوية أو الرقم القومي']);
        }

        $result = [
            'success'           => true,
            'member_id'         => null,
            'membership_number' => $membershipNumber,
            'national_id'       => $nationalId,
            'is_member'         => false,
            'effective_type'    => 'non_member',
            'reason'            => null,
            'name'              => null,
            'phone'             => null,
            'gender'            => null,
            'date_of_birth'     => null,
            'age'               => null,
        ];

        $membership = null;

        if ($membershipNumber !== '') {
            $membership = MembershipValidationService::checkByMembershipNumber($membershipNumber);
        } elseif ($nationalId !== '' && strlen($nationalId) === 14) {
            $membership = MembershipValidationService::checkByNationalId($nationalId);
            $nidParsed = NationalIdParser::parse($nationalId);
            if ($nidParsed && ($nidParsed['is_valid'] ?? false)) {
                $result['date_of_birth'] = $nidParsed['dob'] ?? null;
                $result['age'] = $nidParsed['age_years'] ?? null;
                $result['gender'] = $nidParsed['gender'] ?? null;
            }
        }

        if ($membership) {
            $result['effective_type'] = $membership['effective_type'];
            $result['is_member'] = $membership['is_active_member'];
            $result['member_id'] = $membership['member_id'];
            $result['reason'] = $membership['reason'];

            if ($membership['found'] && $membership['member']) {
                $m = $membership['member'];
                $result['name'] = $m['full_name_ar'] ?? null;
                $result['phone'] = $m['phone_mobile'] ?? null;
                $result['membership_number'] = $m['membership_number'] ?? null;
                $result['national_id'] = $m['national_id'] ?? $result['national_id'];
                if (!empty($m['date_of_birth'])) {
                    $result['date_of_birth'] = $m['date_of_birth'];
                    $result['age'] = (int) date_diff(date_create($m['date_of_birth']), date_create('today'))->y;
                }
                if (!empty($m['gender'])) {
                    $result['gender'] = $m['gender'];
                }
            }
        }

        return $this->json($result);
    }
}
