<?php

namespace App\Http\Controllers\Mahasiswa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Skp2Ver;
use App\Models\KategoriSKP;
use App\DataTables\Mahasiswa\SkpDataTable;
use App\Services\GoogleDriveService;

class SkpController extends Controller
{
    
    public function index(SkpDataTable $dataTable)
    {
        $kategoriSkp = KategoriSKP::with('kegiatan_skp')->get();
        
        return $dataTable->render('mahasiswa.kolokium.skp', compact('kategoriSkp'));
    }

    public function show($id)
    {
        $skp = Skp2Ver::with('kegiatan_skp')->findOrFail($id);

        return response()->json($skp);
    }

    public function store(Request $request)
    {
        $user = auth()->user()->id;

        $validated = $request->validate([
            'judul' => 'required|max:255',
            'nomor_sertifikat' => 'nullable|max:255',
            'lokasi' => 'required|max:255',
            'tanggal_mulai' => 'nullable|date',
            'tanggal_akhir' => 'nullable|date',
            'sertifikat' => 'required|mimes:pdf|max:10240',
            'kegiatan' => 'required|exists:kegiatan_skp,id',
        ]);

        $file = $validated['sertifikat'];
        $filePath = $file->getPathname();
        $fileName = $file->getClientOriginalName();
        $mimeType = $file->getMimeType();
        
        $googleDriveService = new GoogleDriveService();
        $fileId = $googleDriveService->uploadFile($filePath, $fileName, $mimeType);

        Skp2Ver::create([
            'user_id' => $user,
            'judul' => $validated['judul'],
            'nomor_sertifikat' => $validated['nomor_sertifikat'],
            'lokasi' => $validated['lokasi'],
            'tanggal_mulai' => $validated['tanggal_mulai'],
            'tanggal_akhir' => $validated['tanggal_akhir'],
            'sertifikat' => $fileId,
            'kegiatan_skp_id' => $validated['kegiatan'],
        ]);

        return response()->json([
            'success' => true,
            'message' => 'SKP berhasil diajukan!',
        ]);
    }

    public function update(Request $request, $id)
    {
        $skp = Skp2Ver::find($id);

        $validated = $request->validate([
            'judul' => 'required|max:255',
            'nomor_sertifikat' => 'nullable|max:255',
            'lokasi' => 'required|max:255',
            'tanggal_mulai' => 'nullable|date',
            'tanggal_akhir' => 'nullable|date',
            'sertifikat' => 'nullable|mimes:pdf|max:10240',
            'kegiatan' => 'required|exists:kegiatan_skp,id',
        ]);

        $googleDriveService = new GoogleDriveService();

        if ($request->hasFile('sertifikat')) {
            if (!empty($skp->sertifikat)) {
                $googleDriveService->deleteFile($skp->sertifikat);
            }
            $file = $validated['sertifikat'];
            $filePath = $file->getPathname();
            $fileName = $file->getClientOriginalName();
            $mimeType = $file->getMimeType();

            $fileId = $googleDriveService->uploadFile($filePath, $fileName, $mimeType);
            $skp->sertifikat = $fileId;
        }

        $skp->update([
            'judul' => $validated['judul'],
            'nomor_sertifikat' => $validated['nomor_sertifikat'],
            'lokasi' => $validated['lokasi'], 
            'tanggal_mulai' => $validated['tanggal_mulai'],
            'tanggal_akhir' => $validated['tanggal_akhir'],
            'sertifikat' => $skp->sertifikat,
            'kegiatan_skp_id' => $validated['kegiatan'],
        ]);

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

    public function delete($id)
    {
        $skp = Skp2Ver::findOrFail($id);

        $googleDriveService = new GoogleDriveService();
        if (!empty($skp->sertifikat)) {
            $googleDriveService->deleteFile($skp->sertifikat);
        }

        $skp->delete();

        return response()->json();
    }
}
