<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\Concerns\BuildsPortalPayloads;
use App\Http\Controllers\Controller;
use App\Models\FeeChallan;
use App\Models\Student;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

/**
 * Mobile API for the Parent role. All data is scoped to the parent's linked family.
 */
class ParentController extends Controller
{
    use BuildsPortalPayloads;

    private function family(Request $request)
    {
        $family = $request->user()->family;
        abort_if(! $family, 403, 'No family is linked to this account.');

        return $family;
    }

    private function child(Request $request, Student $student): Student
    {
        abort_unless($student->family_id === $this->family($request)->id, 403);

        return $student;
    }

    public function dashboard(Request $request): JsonResponse
    {
        $family = $this->family($request);
        $children = $family->students()->with(['schoolClass', 'section'])->get();
        $fees = $this->challanListPayload($family->id);

        return response()->json([
            'family' => [
                'code' => $family->family_code,
                'father_name' => $family->father_name,
                'guardian' => $family->guardian_name,
            ],
            'children' => $children->map(fn ($s) => $this->childCardPayload($s))->values(),
            'outstanding' => $fees['outstanding'],
            'notices' => $this->noticesPayload($family->branch_id, 'parents', 5),
        ]);
    }

    public function children(Request $request): JsonResponse
    {
        $children = $this->family($request)->students()->with(['schoolClass', 'section'])->get();

        return response()->json(['children' => $children->map(fn ($s) => $this->studentPayload($s))->values()]);
    }

    public function attendance(Request $request, Student $student): JsonResponse
    {
        $this->child($request, $student);
        $month = $request->input('month', Carbon::today()->format('Y-m'));

        return response()->json($this->attendancePayload($student, $month));
    }

    public function results(Request $request, Student $student): JsonResponse
    {
        $this->child($request, $student);

        return response()->json($this->resultPayload($student, $request->integer('exam_id') ?: null));
    }

    public function fees(Request $request): JsonResponse
    {
        return response()->json($this->challanListPayload($this->family($request)->id));
    }

    public function feeDetail(Request $request, FeeChallan $challan): JsonResponse
    {
        abort_unless($challan->family_id === $this->family($request)->id, 403);

        return response()->json($this->challanDetailPayload($challan));
    }

    public function notices(Request $request): JsonResponse
    {
        $family = $this->family($request);

        return response()->json(['notices' => $this->noticesPayload($family->branch_id, 'parents', 50)]);
    }

    public function profile(Request $request): JsonResponse
    {
        $family = $this->family($request)->load(['students.schoolClass', 'students.section']);

        return response()->json([
            'account' => ['name' => $request->user()->name, 'email' => $request->user()->email],
            'family' => [
                'code' => $family->family_code,
                'father_name' => $family->father_name,
                'mother_name' => $family->mother_name,
                'guardian_name' => $family->guardian_name,
                'father_mobile' => $family->father_mobile,
                'address' => $family->address,
            ],
            'children' => $family->students->map(fn ($s) => $this->studentPayload($s))->values(),
        ]);
    }
}
