<?php

namespace App\Http\Controllers;

use App\Models\Pemesanan;
use Auth;
use Illuminate\Http\Request;

class PemesananController extends Controller
{
    public function showAll()
    {
        try {
            $pemesanan = Pemesanan::all();
            return response()->json($pemesanan);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to get all pemesanan', 'error' => $e->getMessage()], 500);
        }
    }

    public function showByUserId($id)
    {
        try {
            $pemesanan = Pemesanan::where('user_id', $id)->get();
            return response()->json($pemesanan);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to get pemesanan by user id', 'error' => $e->getMessage()], 500);
        }
    }

    public function createPemesanan(Request $request, $id)
    {
        try {
            $validatedData = $request->validate([
                'handle_by' => 'nullable',
                'service_id' => 'required',
                'desc' => 'nullable',
                'no_conf' => 'required'
            ]);
            $pemesanan = Pemesanan::create([
                'user_id' => $request->id,
                'handle_by' => $validatedData['handle_by'],
                'service_id' => $validatedData['service_id'],
                'status' => 'pending',
                'desc' => $validatedData['desc'],
                'no_conf' => $validatedData['no_conf']
            ]);

            return response()->json(['message' => 'Pemesanan created successfully', 'data' => $pemesanan], 201);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to create Pemesanan', 'error' => $e->getMessage()], 500);
        }
    }

    public function updateDT(Request $request, $id)
    {
        try {
            $pemesanan = Pemesanan::find($id);
            if (!$pemesanan) {
                return response()->json(['message' => 'Pemesanan not found'], 404);
            }
            $pemesanan->update($request->all());
            return response()->json(['message' => 'Pemesanan updated successfully', 'data' => $pemesanan], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to update Pemesanan', 'error' => $e->getMessage()], 500);
        }
    }

    public function updateDTUsr(Request $request, $id)
    {
        try {
            $pemesanan = Pemesanan::find($id);
            if (!$pemesanan) {
                return response()->json(['message' => 'Pemesanan not found'], 404);
            }
            $pemesanan->update($request->except('status', 'handle_by'));
            return response()->json(['message' => 'Pemesanan updated successfully', 'data' => $pemesanan], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to update Pemesanan', 'error' => $e->getMessage()], 500);
        }
    }

    public function deletePemesanan($id)
    {
        try {
            $pemesanan = Pemesanan::find($id);
            if (!$pemesanan) {
                return response()->json(['message' => 'Pemesanan not found'], 404);
            }
            $pemesanan->delete();
            return response()->json(['message' => 'Pemesanan deleted successfully'], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to delete Pemesanan', 'error' => $e->getMessage()], 500);
        }
    }


    public function deletePemesananBId($id)
    {
        try {
            $pemesanan = Pemesanan::find($id);

            // Menghapus pemesanan jika semua kondisi terpenuhi
            $pemesanan->delete();

            // Mengembalikan respons JSON yang menyatakan pemesanan berhasil dihapus
            return response()->json(['message' => 'Pemesanan deleted successfully'], 200);
        } catch (\Exception $e) {
            // Mengembalikan respons JSON yang menyatakan gagal menghapus pemesanan jika terjadi kesalahan
            return response()->json(['message' => 'Failed to delete Pemesanan', 'error' => $e->getMessage()], 500);
        }
    }
}
