<?php

namespace App\Http\Controllers\Mobile;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class IzinController extends Controller
{
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required|integer|exists:users,id',
            'tanggal' => 'required|date',
            'jenis' => 'required|in:Izin,Sakit,Cuti',
            'keterangan' => 'required|string',
            'bukti' => 'required|image|mimes:jpg,jpeg,png|max:2048'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'ok' => false,
                'message' => $validator->errors()->first()
            ], 422);
        }

        try {

            DB::beginTransaction();

            // ==========================
            // 1️⃣ Upload bukti
            // ==========================
            $file = $request->file('bukti');
            $path = $file->store('bukti_izin', 'public');

            // ==========================
            // 2️⃣ INSERT KE ATTENDANCES DULU
            // ==========================
            $attendanceId = DB::table('attendances')->insertGetId([
                'user_id' => $request->user_id,
                'date' => $request->tanggal,
                'check_in' => null,
                'check_out' => null,
                'status' => strtolower($request->jenis), // izin/sakit/cuti
                'lates_minutes' => null,
                'notes' => null,
                'device_info' => null,
                'location' => null,
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            // ==========================
            // 3️⃣ INSERT KE PENGAJUAN
            // ==========================
            $pengajuanId = DB::table('pengajuan')->insertGetId([
                'id_user' => $request->user_id,
                'id_attendance' => $attendanceId, // 🔥 relasi ke attendance
                'date' => $request->tanggal,
                'jenis' => $request->jenis,
                'keterangan' => $request->keterangan,
                'url_bukti' => $path,
                'status' => 'diajukan',
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            DB::commit();

            return response()->json([
                'ok' => true,
                'message' => 'Pengajuan berhasil dikirim',
                'data' => [
                    'attendance_id' => $attendanceId,
                    'pengajuan_id' => $pengajuanId,
                    'status' => 'diproses'
                ]
            ]);

        } catch (\Exception $e) {

            DB::rollBack();

            return response()->json([
                'ok' => false,
                'message' => 'Terjadi kesalahan: ' . $e->getMessage()
            ], 500);
        }
    }
    public function validasi(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'status' => 'required|in:diterima,ditolak'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'ok' => false,
                'message' => $validator->errors()->first()
            ], 422);
        }

        $attendance = DB::table('attendances')->where('id', $id)->first();

        if (!$attendance) {
            return response()->json([
                'ok' => false,
                'message' => 'Data tidak ditemukan'
            ], 404);
        }

        DB::table('attendances')
            ->where('id', $id)
            ->update([
                'status' => $request->status,
                'updated_at' => now()
            ]);

        return response()->json([
            'ok' => true,
            'message' => 'Status berhasil diperbarui menjadi ' . $request->status
        ]);
    }
}