<?php

namespace App\Filament\Resources\AttendanceResource\Pages;

use App\Filament\Resources\AttendanceResource;
use App\Models\Attendance;
use Carbon\Carbon;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Http\Request;

class ViewAttendance extends ViewRecord
{
    protected static string $resource = AttendanceResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\EditAction::make()
                ->icon('heroicon-o-pencil-square'),
            Actions\DeleteAction::make()
                ->icon('heroicon-o-trash'),
        ];
    }

    public function updateCoordinates(Request $request, Attendance $attendance)
    {
        $user = auth()->user();
        $company = $user->company;

        // Check permissions - only super admin can edit others' attendance
        $isSuperAdmin = $user->hasRole('super_admin');
        $isOwnAttendance = $attendance->user_id == $user->id;
        $backdateEnabled = $company->backdate_enabled ?? false;
        $editableEnabled = $company->allow_editable_location ?? false;

        // Validate permissions
        if (!$isSuperAdmin && !$isOwnAttendance) {
            return response()->json([
                'success' => false,
                'message' => 'Anda hanya dapat mengedit presensi milik Anda sendiri',
            ], 403);
        }

        if (!$backdateEnabled) {
            return response()->json([
                'success' => false,
                'message' => 'Fitur edit presensi tidak diaktifkan',
            ], 403);
        }

        if (!$editableEnabled) {
            return response()->json([
                'success' => false,
                'message' => 'Fitur edit lokasi tidak diaktifkan',
            ], 403);
        }

        // Note: Tidak ada batasan periode cutoff untuk edit koordinat map

        // Validate input
        $request->validate([
            'check_in_latitude' => 'nullable|numeric|between:-90,90',
            'check_in_longitude' => 'nullable|numeric|between:-180,180',
            'check_out_latitude' => 'nullable|numeric|between:-90,90',
            'check_out_longitude' => 'nullable|numeric|between:-180,180',
        ]);

        // Update coordinates
        $updateData = [];
        if ($request->has('check_in_latitude') && $request->has('check_in_longitude')) {
            $updateData['check_in_latitude'] = $request->check_in_latitude;
            $updateData['check_in_longitude'] = $request->check_in_longitude;
        }
        if ($request->has('check_out_latitude') && $request->has('check_out_longitude')) {
            $updateData['check_out_latitude'] = $request->check_out_latitude;
            $updateData['check_out_longitude'] = $request->check_out_longitude;
        }

        if (empty($updateData)) {
            return response()->json([
                'success' => false,
                'message' => 'Tidak ada koordinat yang diupdate',
            ], 400);
        }

        // Update attendance
        $attendance->update($updateData);

        Notification::make()
            ->title('Berhasil')
            ->body('Koordinat berhasil diperbarui.')
            ->success()
            ->send();

        return response()->json([
            'success' => true,
            'message' => 'Koordinat berhasil diperbarui',
        ]);
    }
}
