<?php

namespace App\Http\Controllers\Backend\Attendance;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\HRM\Attendance\DutySchedule;
use App\Repositories\DutyScheduleRepository;
use App\Helpers\CoreApp\Traits\AuthorInfoTrait;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use App\Models\coreApp\Relationship\RelationshipTrait;

class DutyScheduleController extends Controller
{
    use RelationshipTrait, AuthorInfoTrait, ApiReturnFormatTrait;

    protected $dutyScheduleRepository;
    protected $model;

    public function __construct(DutyScheduleRepository $dutyScheduleRepository, DutySchedule $dutySchedule)
    {
        $this->dutyScheduleRepository = $dutyScheduleRepository;
        $this->model = $dutySchedule;
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            return $this->dutyScheduleRepository->table($request);
        }
        $data['table']    = route('dutySchedule.index');
        $data['url_id']    = 'duty_schedule_table_url';
        $data['class']     = 'table_class';
        $data['delete_url'] =  route('dutySchedule.deleteData');
        $data['checkbox'] = true;
        $data['fields'] = $this->dutyScheduleRepository->fields();

        $data['title'] = _trans('common.Duty Schedule');

        return view('backend.attendance.duty_schedule.index', compact('data'));
    }

    public function create()
    {
        $data = $this->dutyScheduleRepository->create();
        return view('backend.attendance.duty_schedule.create', compact('data'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'shift'         => ['required', 'unique:duty_schedules,shift'],
            'start_time'    => 'required',
            'end_time'      => 'required',
            'consider_time' => 'numeric',
            'status_id'     => 'required'
        ]);

        try {
            $this->dutyScheduleRepository->store($request);

            Toastr::success(_trans('response.Operation successful'), 'Success');

            return redirect()->route('dutySchedule.index');
        } catch (\Throwable $th) {
            info($th->getMessage());
            Toastr::error(_trans('response.Something went wrong'), 'Error');
            return redirect()->back();
        }
    }

    public function show($duty_schedule)
    {
        $data = $this->dutyScheduleRepository->show($duty_schedule);
        return view('backend.attendance.duty_schedule.edit', compact('data'));
    }

    public function update(Request $request, DutySchedule $schedule)
    {
        $this->validate($request, [
            'shift'         => ['required', 'unique:duty_schedules,shift,' . $schedule->id],
            'start_time'    => 'required|date_format:H:i:s',
            'end_time'      => 'required|date_format:H:i:s',
            'consider_time' => 'numeric',
            'status_id'     => 'required'
        ]);


        try {
            $this->dutyScheduleRepository->update($request, $schedule);

            Toastr::success(_trans('response.Operation successful'), 'Success');
            return redirect()->route('dutySchedule.index');
        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong'), 'Error');
            return redirect()->back();
        }
    }

    public function delete($duty_schedule)
    {
        try {
            $this->dutyScheduleRepository->distroy($duty_schedule);
            Toastr::success(__translate('attendance.Duty schedule has been deleted'), 'Success');
            return redirect()->back();
        } catch (\Exception $exception) {
            Toastr::error(__translate('attendance.Something went wrong'), 'Error');
            return redirect()->back();
        }
    }

    public function deleteData(Request $request)
    {
        if (demoCheck()) {
            return $this->responseWithError(_trans('message.You cannot delete for demo'), [], 400);
        }
        return $this->dutyScheduleRepository->destroyAll($request);
    }

    public function bulkCreate()
    {
        $data['title'] = 'Create Duty Schedule';

        return view('backend.attendance.duty_schedule.bulk_create', compact('data'));
    }

    public function bulkStore(Request $request)
    {
        $request->validate([
            'file' => 'required|file|mimes:csv,txt|max:2048',
        ], [
            'file.required' => 'Please upload a CSV file.',
        ]);

        try {
            $this->dutyScheduleRepository->bulkStore($request);

            return redirect()->back()->with('success', 'Schedule import successfully.');
        } catch (\Exception $exception) {
            info($exception->getMessage());
            return redirect()->back()->withErrors(['errors' => $exception->getMessage()]);
        }
    }
}
