<?php

namespace App\Http\Controllers\Backend\Hr\Attend;

use App\Http\Controllers\Controller;
use App\Models\Hr\Attend\Holiday;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class HolidayController extends Controller
{
    public function index() {
        // if (Auth::id() == 1) {
        //     $organizations = Holiday::orderBy('id', 'desc')->get();
        // } else {
        //     $organizations = User::findOrfail(Auth::id())->organizations;
        // }

        $session_data = DB::table('organization_session as ac')->get();
        $holidays = DB::table('holiday_calendar as ac')->leftJoin('holiday_calendar_details as acd', 'acd.hd_id', '=', 'ac.hd_id')
        ->leftJoin('organization_session as oss', 'oss.session_id', '=', 'ac.session_id')
        ->select('ac.*', 'oss.*', DB::raw("group_concat(DATE_FORMAT(acd.holiday, '%d/%m/%Y') SEPARATOR ', ') as holidayss"))->groupBy('acd.hd_id')->get();
            // ->where('acd.holiday','LIKE',"$year-%")->where('ac.approval_status', 2)
        return view('backend.hr.attend.holiday', compact('holidays', 'session_data'));
    }
    public function store(Request $request) {
        $this->validate($request, [
            'session_id' => 'required|int|max:10',
            'adate' => 'required|string|max:10',
            'adate2' => 'required|string|max:10',
            'atext' => 'required|string|max:250',

            // 'adate' => 'date',
            // 'adate2' => 'date',
            // 'date_field' => 'after:adate|before:adate2',
        ]);
        if($request->adate > $request->adate2) {
            return redirect()->route('holidays.index')->with(['alert-type' => 'error', 'message' => 'Date input error']);
        }

        if($request->hd_id) {
            Holiday::findOrFail($request->hd_id)->update([
                'session_id' => $request->session_id,
                'adate' => $request->adate,
                'adate2' => $request->adate2,
                'atext' => $request->atext,
                'edited_by' => Auth::user()->id,
                'edited_on' => date('Y-m-d H:i:s')
            ]);
            DB::table('holiday_calendar_details')->where('hd_id', $request->hd_id)->delete();
            $hd_id = $request->hd_id;
        } else {
            Holiday::create([
                'session_id' => $request->session_id,
                'adate' => $request->adate,
                'adate2' => $request->adate2,
                'atext' => $request->atext,
                'approval_status' => 2,
                'added_by' => Auth::user()->id,
                'added_on' => date('Y-m-d H:i:s')
            ]);
            $hd_id = DB::table('holiday_calendar')->max('hd_id');
        }

        $start      =Carbon::parse($request->adate);
        $end        =Carbon::parse($request->adate2);
        $date_diff  =$start->diffInDays($end);

        $detailData = array();
        $incr = 0;
        for($i = 0; $i <= $date_diff; $i++) {
            $detailData[$incr] = array(
                'hd_id' => $hd_id,
                'holiday' => (clone $start)->addDays($i)->format('Y-m-d')
            );
            $incr++;
        }
        if($detailData) {
            DB::table('holiday_calendar_details')->insert($detailData);
        }

        return redirect()->route('holidays.index')->with(['alert-type' => 'success', 'message' => 'Holiday successfully saved']);
    }

    public function edit(int $id) {
        return Holiday::findOrFail($id);
    }

    public function approve(int $hd_id): RedirectResponse {
        $dta = Holiday::findOrFail($hd_id);
        $msg = '';
        if($dta and $dta->approval_status < 2) {
            Holiday::findOrFail($hd_id)->update([
                'approval_status' => 2,
                'edited_by' => Auth::user()->id,
                'edited_on' => date('Y-m-d H:i:s')
            ]);
            $msg = 'Holiday successfully approved';
            return redirect()->back()->with(['alert-type' => 'success', 'message' => $msg]);
        } else {
            $msg = 'Holiday already approved / removed';
            return redirect()->back()->with(['alert-type' => 'error', 'message' => $msg]);
        }
    }

    public function destroy(int $id): RedirectResponse {
        Holiday::findOrFail($id)->delete();
        return redirect()->back()->with(['alert-type' => 'success', 'message' => 'Holiday successfully deleted']);
    }
}
