<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Artisan;

class PengaturanSistemController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        // Bisa diganti dengan model Pengaturan jika ada
        $pengaturan = null;

        return view('admin.pengaturan.index', compact('user', 'pengaturan'));
    }

    public function updateAplikasi(Request $request)
    {
        $request->validate([
            'nama_aplikasi' => 'required|string|max:100',
            'deskripsi'     => 'nullable|string|max:200',
            'email_kontak'  => 'nullable|email',
            'telepon'       => 'nullable|string|max:20',
        ]);

        // Simpan ke config/session (bisa disesuaikan ke DB jika punya tabel pengaturan)
        session([
            'pengaturan.nama_aplikasi' => $request->nama_aplikasi,
            'pengaturan.deskripsi'     => $request->deskripsi,
            'pengaturan.email_kontak'  => $request->email_kontak,
            'pengaturan.telepon'       => $request->telepon,
        ]);

        return back()->with('success', 'Pengaturan aplikasi berhasil disimpan.');
    }

    public function updateJamKerja(Request $request)
    {
        $request->validate([
            'jam_masuk_default'  => 'required',
            'jam_pulang_default' => 'required',
            'toleransi_menit'    => 'required|integer|min:0|max:120',
        ]);

        session([
            'pengaturan.jam_masuk_default'  => $request->jam_masuk_default,
            'pengaturan.jam_pulang_default' => $request->jam_pulang_default,
            'pengaturan.toleransi_menit'    => $request->toleransi_menit,
        ]);

        return back()->with('success', 'Jam kerja default berhasil disimpan.');
    }

    public function updateProfil(Request $request)
    {
        $user = Auth::user();

        $request->validate([
            'name'  => 'required|string|max:100',
            'email' => 'required|email|unique:users,email,' . $user->id,
        ]);

        $user->update([
            'name'  => $request->name,
            'email' => $request->email,
        ]);

        return back()->with('success', 'Profil admin berhasil diperbarui.');
    }

    public function updatePassword(Request $request)
    {
        $request->validate([
            'current_password' => 'required',
            'password'         => 'required|min:8|confirmed',
        ]);

        if (!Hash::check($request->current_password, Auth::user()->password)) {
            return back()->withErrors(['current_password' => 'Password saat ini tidak sesuai.']);
        }

        Auth::user()->update([
            'password' => Hash::make($request->password),
        ]);

        return back()->with('success', 'Password berhasil diperbarui.');
    }

    public function clearCache()
    {
        Artisan::call('cache:clear');
        Artisan::call('view:clear');
        Artisan::call('config:clear');
        Artisan::call('route:clear');

        return redirect()->route('admin.pengaturan.index')
            ->with('success', 'Cache berhasil dibersihkan.');
    }
}   