<?php

namespace App\Http\Controllers;

use App\Http\Requests\HukumRequest;
use App\Models\Hukum;
use App\Models\JenisSanksi;
use App\Models\Penaatan;
use App\Models\Perusahaan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;

class HukumController extends Controller
{
    public function index()
    {
        try {
            $hukumData = Hukum::with(['perusahaan', 'jenisSanksi', 'penaatan'])->orderBy('created_at', 'desc')->get();
            return Inertia::render('admin/hukum/index_hukum', [
                'hukumData' => $hukumData,
                'perusahaan' => Perusahaan::all(),
                'jenisSanksi' => JenisSanksi::all(),
                'penaatan' => Penaatan::all(),
            ]);
        } catch (\Exception $e) {
            Log::error('Error fetching data: ' . $e->getMessage());
            return back()->with('error', 'Terjadi kesalahan saat memuat data.');
        }
    }


    public function store(HukumRequest $request)
    {
    try {
        DB::beginTransaction();

        $data = $request->validated();

        if ($request->hasFile('SanksiFile')) {
            $fileName = time() . '_' . $request->file('SanksiFile')->getClientOriginalName();
            $path = $request->file('SanksiFile')->storeAs('files/hukum/sanksi', $fileName, 'public');
            $data['SanksiFile'] = $path;
        }

        if ($request->hasFile('PenaatanFile')) {
            $fileName = time() . '_' . $request->file('PenaatanFile')->getClientOriginalName();
            $path = $request->file('PenaatanFile')->storeAs('files/hukum/penaatan', $fileName, 'public');
            $data['PenaatanFile'] = $path;
        }

        Hukum::create($data);

        DB::commit();

        return redirect()->route('admin.hukum.index')->with('success', 'Status Hukum berhasil ditambahkan');
    } catch (\Exception $e) {
        DB::rollBack();
        Log::error('Error creating hukum: ' . $e->getMessage());
        return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
    }
    }

    public function update(HukumRequest $request, Hukum $hukum)
    {
        try {
            DB::beginTransaction();

            $data = $request->validated();

            if ($request->hasFile('SanksiFile')) {
                // Delete old file if exists
                if ($hukum->SanksiFile && Storage::disk('public')->exists($hukum->SanksiFile)) {
                    Storage::disk('public')->delete($hukum->SanksiFile);
                }

                $fileName = time() . '_' . $request->file('SanksiFile')->getClientOriginalName();
                $path = $request->file('SanksiFile')->storeAs('files/hukum/sanksi', $fileName, 'public');
                $data['SanksiFile'] = $path;
            }

            if ($request->hasFile('PenaatanFile')) {
                // Delete old file if exists
                if ($hukum->PenaatanFile && Storage::disk('public')->exists($hukum->PenaatanFile)) {
                    Storage::disk('public')->delete($hukum->PenaatanFile);
                }

                $fileName = time() . '_' . $request->file('PenaatanFile')->getClientOriginalName();
                $path = $request->file('PenaatanFile')->storeAs('files/hukum/penaatan', $fileName, 'public');
                $data['PenaatanFile'] = $path;
            }

            $hukum->update($data);

            DB::commit();

            return redirect()->route('admin.hukum.index')->with('success', 'Status Hukum berhasil diperbarui');
        } catch (\Exception $e) {
            DB::rollBack();
            Log::error('Error updating hukum: ' . $e->getMessage());
            return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
        }
    }

    public function destroy(Hukum $hukum)
    {
        try {
            DB::beginTransaction();

            // Delete file if exists
            if ($hukum->SanksiFile && Storage::disk('public')->exists($hukum->SanksiFile)) {
                Storage::disk('public')->delete($hukum->SanksiFile);
            }

            if ($hukum->PenaatanFile && Storage::disk('public')->exists($hukum->PenaatanFile)) {
                Storage::disk('public')->delete($hukum->PenaatanFile);
            }

            $hukum->delete();

            DB::commit();

            return redirect()->route('admin.hukum.index')->with('success', 'Status Hukum berhasil dihapus');
        } catch (\Exception $e) {
            DB::rollBack();
            Log::error('Error deleting hukum: ' . $e->getMessage());
            return response()->json(['message' => 'Error: ' . $e->getMessage()], 500);
        }
    }

}
