<?php

namespace App\Http\Controllers;

use App\Models\Employee;
use App\Models\EmployeeAccessDetail;
use App\Models\AccessCatalog;
use App\Models\SystemLog;
use Illuminate\Http\Request;

class EmployeeAccessController extends Controller
{
    // Tambah akses ke pegawai
    public function store(Request $request, Employee $employee)
    {
        $validated = $request->validate([
            'access_catalog_id' => 'required|exists:access_catalogs,id',
            'granted_date' => 'required|date',
        ]);

        // Cek duplikat
        $exists = EmployeeAccessDetail::where('employee_id', $employee->id)
            ->where('access_catalog_id', $validated['access_catalog_id'])
            ->exists();

        if ($exists) {
            return back()->with('error', 'Akses ini sudah dimiliki pegawai.');
        }

        $expiryDate = $employee->retirement_date ?? now()->addYear();

        $accessDetail = EmployeeAccessDetail::create([
            'employee_id' => $employee->id,
            'access_catalog_id' => $validated['access_catalog_id'],
            'granted_date' => $validated['granted_date'],
            'original_expiry_date' => $expiryDate,
            'current_expiry_date' => $expiryDate,
            'status' => 'active'
        ]);

        SystemLog::logCreate(
            auth()->id(),
            'employee_access',
            $accessDetail->id,
            $accessDetail->toArray()
        );

        return back()->with('success', 'Akses berhasil ditambahkan.');
    }

    // Hapus akses dari pegawai
    public function destroy(EmployeeAccessDetail $accessDetail)
    {
        $oldValues = $accessDetail->toArray();

        // Hapus semua relasi terkait
        $accessDetail->extensionRequests()->delete();
        $accessDetail->revocationSchedules()->delete();
        $accessDetail->reminderLogs()->delete();
        $accessDetail->revocationLogs()->delete();
        
        $accessDetail->delete();

        SystemLog::logDelete(
            auth()->id(),
            'employee_access',
            $accessDetail->id,
            $oldValues
        );

        return back()->with('success', 'Akses berhasil dihapus.');
    }

    // Get detail akses untuk modal/edit
    public function show(EmployeeAccessDetail $accessDetail)
    {
        $accessDetail->load(['employee', 'accessCatalog', 'revocationSchedules']);
        
        return $this->successResponse($accessDetail);
    }

    // Update tanggal granted
    public function update(Request $request, EmployeeAccessDetail $accessDetail)
    {
        $validated = $request->validate([
            'granted_date' => 'required|date',
        ]);

        $oldValues = $accessDetail->toArray();
        $accessDetail->update($validated);

        SystemLog::logUpdate(
            auth()->id(),
            'employee_access',
            $accessDetail->id,
            $oldValues,
            $accessDetail->fresh()->toArray()
        );

        return back()->with('success', 'Akses berhasil diupdate.');
    }
}