<?php

namespace App\Http\Controllers;

use App\Mail\SendUniqueCode;
use App\Models\BookingModel;
use Carbon\Carbon;
use Generator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class BookingController extends Controller
{
    /**
     * Getch Data Booking
     */
    public function getchData() {
        $data = BookingModel::get();

        return response()->json([
            'status' => true,
            'data' => $data->toArray()
        ]);
    }

    /**
     * Random Char and Number
     */
    public function generateCode() {
        $kode_unik = '';
        $charABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        for($i = 0; $i < 6; $i++) {
            $randNum = random_int(0, 1);
            if($randNum == 0) {
                $kode_unik .= $charABC[random_int(0, strlen($charABC) - 1)];
            } else if($randNum == 1) {
                $kode_unik .= str(random_int(0, 9));
            }
        }

        $dataAvl = BookingModel::find($kode_unik, 'kode_unik');
        if($dataAvl != null) {
            $this->generateCode();
        } else {
            return $kode_unik;
        }
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('admin.pages.data_booking.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $request->validate([
                'nama_lengkap' => ['required', 'string'],
                'email' => ['required', 'string'],
                'no_telp' => ['required', 'string'],
                'tgl_booking' => ['required', 'string'],
                'deskripsi_tambahan' => ['required', 'string']
            ]);

            $kode_unik = $this->generateCode();
            $tgl_request = Carbon::parse($request->tgl_booking)->timestamp + 86399;

            BookingModel::create([
                'nama_lengkap' => $request->nama_lengkap,
                'email' => $request->email,
                'no_telp' => $request->no_telp,
                'tgl_booking' => $request->tgl_booking,
                'deskripsi_tambahan' => $request->deskripsi_tambahan,
                'kode_unik' => $kode_unik,
                'unix_expired_time' => $tgl_request
            ]);

            Mail::to($request->email)->send(new SendUniqueCode(
                $request->nama_lengkap, 
                $kode_unik,
                date('H:i:s', $tgl_request - 86399),
                date('H:i:s', $tgl_request),
                $request->tgl_booking,
            ));

            return response()->json([
                'status' => true,
                'msg' => 'Booking success!'
            ]);
        } catch(\Exception $e) {
            return response()->json([
                'status' => false,
                'msg' => $e->getMessage()
            ]);
        }

    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $data = BookingModel::where('id_booking', $id)->first();
        return response()->json([
            'status' => true,
            'data' => $data
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $data = BookingModel::where('id_booking', $id)->first();
        return response()->json([
            'status' => true,
            'data' => $data
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $request->validate([
            'status_tes' => ['required']
        ]);

        BookingModel::where('id_booking', $id)->update([
            'status_tes' => $request->status_tes
        ]);

        return response()->json([
            'status' => true,
            'msg' => 'Data berhasil diedit'
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        BookingModel::where('id_booking', $id)->delete();

        return response()->json([
            'status' => true,
            'msg' => 'Data berhasil dihapus'
        ]);
    }
}
