<?php

namespace Modules\Travel\Http\Controllers;

use App\Models\Travel\TravelType;
use Illuminate\Routing\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Modules\Travel\Entities\TravelExpense;
use App\Models\Expenses\IncomeExpenseCategory;
use Illuminate\Http\Request;
use Modules\Travel\Entities\TravelExpenseLog;
use Modules\Travel\Http\Requests\TravelExpenseRequest;
use Modules\Travel\Http\Requests\TravelMeetingRequest;
use Modules\Travel\Http\Enums\ModeOfTransportationEnum;
use Modules\Travel\Repositories\TravelExpenseRepository;

class TravelExpenseController extends Controller
{
    protected $travelExpenseRepo;

    public function __construct()
    {
        $this->travelExpenseRepo = new TravelExpenseRepository;
    }

    public function index(Request $request, $plan_id)
    {
        try {
            if ($request->ajax()) {
                return $this->travelExpenseRepo->table($request, $plan_id);
            }
            $data['class']  = 'travel_plan_expense_datatable';
            $data['fields'] = $this->travelExpenseRepo->fields();
            $data['table']  = route('travel.plans.expenses.index', $plan_id);
            $data['url_id'] = 'travel_plan_expense_datatable_url';
            $data['title'] = _trans('common.Travel Expense List');
            $data['plan_id'] = $plan_id;

            return view('travel::travel-expenses.index', compact('data'));
        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return redirect()->back();
        }
    }

    public function create($plan_id)
    {
        $data['title']   = _trans('common.Travel Expense Create');
        $data['plan_id'] = $plan_id;
        $data['modeOfTransportations'] = array_map(fn(ModeOfTransportationEnum $mode) => $mode->value, ModeOfTransportationEnum::cases());
        $data['expenseCategories'] = IncomeExpenseCategory::where('company_id', auth()->user()->company_id)->where('status_id', 1)->where('is_income', 0)->where('type', 'Travel')->get(['id', 'name']);

        return view('travel::travel-expenses.create', compact('data'));
    }

    public function store(TravelExpenseRequest $request, $plan_id)
    {
        try {

            $this->travelExpenseRepo->storeTravelExpense($request, $plan_id);

            Toastr::success(_trans('response.Travel Expense has been created successfully'), 'Success');
            return redirect()->route('travel.plans.expenses.index', $plan_id);

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        }        
    }

    public function show($plan_id, $id)
    {
        $data['title'] = _trans('common.Travel Expense Details');
        $data['travelExpense'] = TravelExpense::with('employee', 'travelExpenseLogs')->findOrFail($id);
        return view('travel::travel-expenses.show', compact('data'));
    }

    public function edit($plan_id, $id)
    {
        $data['title']             = _trans('common.Travel Expense Edit');
        $data['plan_id']           = $plan_id;
        $data['travelExpense']     = TravelExpense::with('travelExpenseLogs.expenseCategory')->findOrFail($id);
        $data['expenseCategories'] = IncomeExpenseCategory::query()
                                    ->where('company_id', auth()->user()->company_id)
                                    ->where('status_id', 1)
                                    ->where('is_income', 0)
                                    ->where('type', 'Travel')
                                    ->get(['id', 'name']);

        $data['modeOfTransportations'] = array_map(fn(ModeOfTransportationEnum $mode) => $mode->value, ModeOfTransportationEnum::cases());

        return view('travel::travel-expenses.edit', compact('data'));
    }

    public function update(TravelExpenseRequest $request, $plan_id, $id)
    {
        try {

            $travelMeeting = $this->travelExpenseRepo->updateTravelExpense($request, $plan_id, $id);

            Toastr::success(_trans('response.Travel Expense has been updated successfully'), 'Success');
            return redirect()->route('travel.plans.expenses.index', $travelMeeting->travel_plan_id);

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        }   
    }

    public function destroy($id)
    {
        try {

            $this->travelExpenseRepo->delete($id);

            Toastr::success(_trans('response.Travel Expense has been deleted successfully'), 'Success');
            return redirect()->back();

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        } 
    }

    public function approve(Request $request, $id)
    {
        try {

            $this->travelExpenseRepo->approve($request, $id);

            return response()->json(['status' => 1, 'message' => _trans('response.Travel Expense has been approved successfully')]);

        } catch (\Throwable $th) {
            Toastr::error(_trans('response.Something went wrong!'), 'Error');
            return back();
        } 
    }
}
