<?php

namespace App\Http\Controllers\Api\Frontend;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Resources\Frontend\FrontendPageResource;
use App\Http\Resources\Frontend\FrontendUserResource;
use App\Services\Frontend\FrontendPageService;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class FrontendController extends BaseApiController
{
    public function __construct(private FrontendPageService $pageService)
    {
        parent::__construct();
    }

    public function index(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('index')),
        ]);
    }

    public function facility(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('facility')),
        ]);
    }

    public function team(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('team')),
        ]);
    }

    public function callToAction(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('call_to_action')),
        ]);
    }

    public function testimonial(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('testimonial')),
        ]);
    }

    public function notFound(): JsonResponse
    {
        return $this->success([
            'page' => new FrontendPageResource($this->pageService->page('not_found')),
        ]);
    }

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

        return $this->success([
            'user' => new FrontendUserResource($user),
        ]);
    }
}
