<?php

namespace App\Http\Controllers\Backend\Report;

use App\Http\Controllers\Controller;
use App\Models\BookingValue;
use App\Models\Breed;
use App\Models\Organization;
use App\Models\OrganizationForBooking;
use App\Models\Treatment;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class TemporaryReportController extends Controller
{
    /**
     * @param Request $request
     * @request body {'id' => $id}
     * @return Collection
     */
    public function previewsTreatmentHistory(Request $request): Collection
    {
        return DB::table('employees')
            ->leftJoin('treatments', function ($join) {
                $join->on('employees.id', '=', 'treatments.employee_id');
            })->rightJoin('diseases', 'treatments.disease_id', '=', 'diseases.id')
            ->where('employees.id', '=', $request->input('id'))->get();
    }

    /**
     * @request body {'id' => $id}
     * @return Application|Factory|View
     */
    public function BookValueReportView()
    {
        $booking_values = BookingValue::with('breed')->groupBy('breeds_id', 'sex')->get();
        $user = User::where('id',Auth::user()->id)->first();
        if ($user->id != 1){
            $organizationName = Organization::where('id', '=', $user->organizations[0]->id)->first()->name ?? "Organization Name";
        }else{
            $organizationName = Organization::where('id', '=',Auth::user()->id)->first()->name ?? "Organization Name";
        }

        return view('backend.report.book_value.book_value_report', compact('booking_values', 'organizationName'));
    }
}
