<?php

namespace App\Http\Controllers\Api\Settings;

use App\Http\Controllers\Api\Core\BaseApiController;
use App\Http\Requests\Settings\SchoolCalendarIndexRequest;
use App\Http\Requests\Settings\SchoolCalendarOptionsRequest;
use App\Http\Requests\Settings\SchoolCalendarStoreRequest;
use App\Http\Requests\Settings\SchoolCalendarUpdateRequest;
use App\Http\Resources\Settings\SchoolCalendarEventDetailResource;
use App\Http\Resources\Settings\SchoolCalendarEventResource;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarFormatterService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMeetingService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMutationService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class SchoolCalendarController extends BaseApiController
{
    public function __construct(
        private SchoolCalendarContextService $contextService,
        private SchoolCalendarQueryService $queryService,
        private SchoolCalendarMeetingService $meetingService,
        private SchoolCalendarFormatterService $formatterService,
        private SchoolCalendarMutationService $mutationService
    ) {
        parent::__construct();
    }

    public function options(SchoolCalendarOptionsRequest $request): JsonResponse
    {
        return $this->success([
            'event_types' => $this->contextService->eventTypes(),
            'defaults' => [
                'school_year' => $this->contextService->defaultSchoolYear(),
                'semester' => $this->contextService->defaultSemester(),
            ],
        ]);
    }

    public function index(SchoolCalendarIndexRequest $request): JsonResponse
    {
        return $this->respondWithEvents($request->validated());
    }

    /**
     * Legacy teacher portal URL: same response as
     * GET /api/v1/settings/school-calendar/events?audience=teacher
     */
    public function teacherCalendarLegacy(SchoolCalendarIndexRequest $request): JsonResponse
    {
        return $this->respondWithEvents([
            ...$request->validated(),
            'audience' => 'teacher',
        ]);
    }

    private function respondWithEvents(array $filters): JsonResponse
    {
        $audience = $filters['audience'] ?? null;
        $includeMeetings = array_key_exists('include_meetings', $filters)
            ? (bool) $filters['include_meetings']
            : true;

        if (empty($filters['school_year'])) {
            $filters['school_year'] = $this->contextService->defaultSchoolYear();
        }

        $events = $this->queryService->listEvents($filters);
        $events = $this->queryService->filterEventsForAudience($events, $audience);

        $formatted = $events->map(function ($event) use ($audience) {
            return $this->formatterService->formatEvent($event, $audience);
        })->all();

        if ($includeMeetings) {
            $parentUserId = null;
            if ($audience === 'parent') {
                $actor = $this->authenticatedUserIdOrUnauthorized();
                if ($actor instanceof JsonResponse) {
                    return $actor;
                }
                $parentUserId = $actor;
            }
            $meetingRows = $this->meetingService->listMeetings($filters['school_year'] ?? '', $audience, $parentUserId);
            foreach ($meetingRows as $row) {
                $formatted[] = $this->formatterService->formatMeetingEvent($row, $audience);
            }
        }

        return $this->success([
            'events' => SchoolCalendarEventResource::collection($formatted),
        ]);
    }

    public function show(int $eventId): JsonResponse
    {
        $event = $this->queryService->findEvent($eventId);
        if (!$event) {
            return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
        }

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

    public function store(SchoolCalendarStoreRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $targets = $this->extractEmailTargets($payload);

        try {
            $result = $this->mutationService->create($payload, $targets);
        } catch (\Throwable $e) {
            Log::error('SchoolCalendar store failed: ' . $e->getMessage());
            return $this->error('Failed to save event.', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        return $this->success([
            'event' => new SchoolCalendarEventDetailResource($result['event']),
            'email_status' => $result['email_status'] ?? [],
        ], 'Event added successfully.', Response::HTTP_CREATED);
    }

    public function update(SchoolCalendarUpdateRequest $request, int $eventId): JsonResponse
    {
        $event = $this->queryService->findEvent($eventId);
        if (!$event) {
            return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
        }

        $payload = $request->validated();
        $targets = $this->extractEmailTargets($payload);

        try {
            $result = $this->mutationService->update($event, $payload, $targets);
        } catch (\Throwable $e) {
            Log::error('SchoolCalendar update failed: ' . $e->getMessage());
            return $this->error('Failed to update event.', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        return $this->success([
            'event' => new SchoolCalendarEventDetailResource($result['event']),
            'email_status' => $result['email_status'] ?? [],
        ], 'Event updated successfully.');
    }

    public function destroy(int $eventId): JsonResponse
    {
        $event = $this->queryService->findEvent($eventId);
        if (!$event) {
            return $this->error('Event not found.', Response::HTTP_NOT_FOUND);
        }

        try {
            $this->mutationService->delete($event);
        } catch (\Throwable $e) {
            Log::error('SchoolCalendar delete failed: ' . $e->getMessage());
            return $this->error('Failed to delete event.', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        return $this->success(null, 'Event deleted successfully.');
    }

    private function extractEmailTargets(array $payload): array
    {
        return [
            'parents' => !empty($payload['send_email_parent']),
            'teachers' => !empty($payload['send_email_teacher']),
            'admins' => !empty($payload['send_email_admin']),
        ];
    }

    /**
     * @return int|JsonResponse
     */
    private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
    {
        $userId = (int) (auth()->id() ?? 0);
        if ($userId <= 0) {
            return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
        }

        return $userId;
    }
}
