<?php

namespace App\Http\Controllers\Api\Frontend;

use App\Http\Controllers\Api\Core\BaseApiController;
use App\Http\Requests\Frontend\LandingTeacherRequest;
use App\Http\Resources\Frontend\LandingPageResource;
use App\Services\Frontend\LandingPageService;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class LandingPageController extends BaseApiController
{
    public function __construct(private LandingPageService $service)
    {
        parent::__construct();
    }

    public function index(LandingTeacherRequest $request): JsonResponse
    {
        $user = $request->user();
        if (!$user) {
            return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
        }

        $payload = $this->service->dashboardForUser(
            $user,
            $request->validated()['class_section_id'] ?? null
        );

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function teacher(LandingTeacherRequest $request): JsonResponse
    {
        $user = $request->user();
        if (!$user) {
            return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
        }

        $payload = $this->service->teacher(
            $user,
            $request->validated()['class_section_id'] ?? null
        );

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function parent(): JsonResponse
    {
        $user = auth()->user();
        if (!$user) {
            return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
        }

        $payload = $this->service->parent($user);

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function administrator(): JsonResponse
    {
        $payload = $this->service->administrator();

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function admin(): JsonResponse
    {
        $payload = $this->service->admin();

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function student(): JsonResponse
    {
        $payload = $this->service->student();

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }

    public function guest(): JsonResponse
    {
        $payload = $this->service->guest();

        return $this->success([
            'landing' => new LandingPageResource($payload),
        ]);
    }
}
