<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\StudentExamService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class StudentProfileController extends Controller
{
    public function __construct(
        private readonly StudentExamService $examService,
    ) {
    }

    public function show(Request $request): JsonResponse
    {
        return response()->json(
            $this->examService->buildEditableProfilePayload($request->user())
        );
    }

    public function update(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:100'],
            'father_name' => ['required', 'string', 'max:100'],
            'mother_name' => ['required', 'string', 'max:100'],
            'class_id' => ['required', 'integer'],
            'date_of_birth' => ['required', 'date'],
            'sr_number' => ['nullable', 'string', 'max:20'],
            'upi_id' => ['required', 'string', 'max:150'],
            'pan_card_number' => ['required', 'string', 'max:15'],
            'bank_account_number' => ['required', 'string', 'max:50'],
            'bank_account_holder_name' => ['required', 'string', 'max:150'],
            'bank_branch' => ['nullable', 'string', 'max:150'],
            'bank_ifsc_code' => ['required', 'string', 'max:15'],
            'religion' => ['required', 'string', 'max:40'],
            'aadhaar_card_number' => ['required', 'digits:12'],
        ]);

        $result = $this->examService->updateStudentProfile(
            $request->user(),
            $validated,
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'invalid_class') {
            'updated' => 200,
            'invalid_class', 'upi_locked' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function updateImage(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'attachment_id' => ['required', 'integer'],
        ]);

        $result = $this->examService->updateStudentProfileImage(
            $request->user(),
            (int) $validated['attachment_id'],
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'invalid_attachment') {
            'updated' => 200,
            'invalid_attachment' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function changePassword(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'current_password' => ['required', 'string', 'max:190'],
            'new_password' => ['required', 'string', 'min:6', 'max:50', 'same:confirm_password'],
            'confirm_password' => ['required', 'string', 'min:6', 'max:50'],
        ]);

        $result = $this->examService->changeStudentPassword(
            $request->user(),
            (string) $validated['current_password'],
            (string) $validated['new_password'],
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'invalid_current_password') {
            'updated' => 200,
            'invalid_current_password', 'same_password' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function sendMobileOtp(Request $request): JsonResponse
    {
        $result = $this->examService->sendMobileVerificationOtp(
            $request->user(),
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'missing_mobile') {
            'sent', 'already_verified' => 200,
            'missing_mobile' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function verifyMobileOtp(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'otp' => ['required', 'digits:6'],
        ]);

        $result = $this->examService->verifyMobileOtp(
            $request->user(),
            (string) $validated['otp'],
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'invalid_otp') {
            'verified' => 200,
            'invalid_otp' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function sendEmailOtp(Request $request): JsonResponse
    {
        $result = $this->examService->sendEmailVerificationCode(
            $request->user(),
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'missing_email') {
            'sent', 'already_verified' => 200,
            'missing_email' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }

    public function verifyEmailOtp(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'otp' => ['required', 'digits:6'],
        ]);

        $result = $this->examService->verifyEmailCode(
            $request->user(),
            (string) $validated['otp'],
            $request,
            $request->attributes->get('studentDeviceSession'),
        );

        $statusCode = match ($result['status'] ?? 'invalid_otp') {
            'verified' => 200,
            'invalid_otp' => 422,
            default => 422,
        };

        return response()->json($result, $statusCode);
    }
}
