<?php

namespace Modules\SpecialAttendance\Http\Controllers;

use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use App\Http\Controllers\Controller;
use App\Models\Hrm\Department\Department;
use App\Models\User;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\SpecialAttendance\Http\Repositories\DeductionRepository;
use Modules\SpecialAttendance\Http\Requests\DeductionAppealRequest;

class DeductionController extends Controller
{
    use ApiReturnFormatTrait;

    protected $deductionRepository;

    public function __construct(DeductionRepository $deductionRepository)
    {
        $this->deductionRepository = $deductionRepository;
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            return $this->deductionRepository->table($request);
        }
        $data['class'] = 'attendance_deduction_table';
        $data['table'] = route('deduction.index');
        $data['fields'] = $this->deductionRepository->fields();
        $data['url_id'] = 'attendance_deduction_table_table';
        $data['title'] = _trans('common.Attendance Deductions');

        return view('specialattendance::deduction.index', compact('data'));
    }

    public function createModal()
    {
        try {
            $data['title'] = _trans('common.Generate Deduction');
            $data['url'] = route('deduction.store');
            $data['button'] = _trans('common.Generate');
            $data['departments'] = Department::where(currentCompanyCurrentBranch())->pluck('title', 'id');
            $data['employees'] = User::with('designation')->where(currentCompanyCurrentBranch())->get(['id', 'name', 'designation_id']);

            return view('specialattendance::deduction.create-modal', compact('data'));
        } catch (\Throwable $th) {
            return response()->json('fail');
        }
    }

    public function store(Request $request)
    {
        try {
            $this->deductionRepository->storeDeductionFromTardyRecordsWithAbsent($request);

            return $this->responseWithSuccess(_trans('message.Attendance Deduction generated successfully.'), 200);
        } catch (\Throwable $th) {
            info($th->getMessage());
            return $this->responseWithError($th->getMessage(), [], 400);
        }
    }

    public function show($id)
    {
        $data = $this->deductionRepository->show($id);

        return view('specialattendance::deduction.show', compact('data'));
    }

    public function consider(Request $request, $id)
    {
        try {
            $this->deductionRepository->consider($id);
            Toastr::success(_trans('message.Deduction considered successfully.'), 'Success');

            return redirect()->route('deduction.index');
        } catch (\Exception $e) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');

            return redirect()->back();
        }
    }

    public function appeal(DeductionAppealRequest $request, $id)
    {
        try {
            $this->deductionRepository->appeal($id);
            Toastr::success(_trans('message.Appeal has been sent successfully.'), 'Success');

            return redirect()->route('deduction.index');
        } catch (\Exception $e) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');

            return redirect()->back();
        }
    }
}
