<?php

namespace App\Modules\HR\Reports\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\PLATFORM\Settings\Models\Branch;
use App\Modules\HR\Core\Models\Department;
use App\Modules\HR\Core\Models\Employee;
use App\Modules\HR\Payroll\Models\PayrollGroup;
use App\Modules\HR\Reports\Services\ReportManager;
use App\Modules\PLATFORM\Settings\Models\PaymentMethod;

class MasterReportController extends Controller
{
    public function employeeDetails(Request $request ) { return $this->view('employee-details'); }
    public function postEmployeeDetails(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeePhoto(Request $request) { return $this->view('employee-photo'); }
    public function postEmployeePhoto(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function employeeBirthday(Request $request) { return $this->view('employee-birthday'); }
    public function postEmployeeBirthday(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function basicSalary(Request $request) { return $this->view('basic-salary'); }
    public function postBasicSalary(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function paymentDetails(Request $request) { return $this->view('payment-details'); }
    public function postPaymentDetails(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function additionDeductionDetails(Request $request) { return $this->view('addition-deduction-details'); }
    public function postAdditionDeductionDetails(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeeRecruitment(Request $request) { return $this->view('employee-recruitment'); }
    public function postEmployeeRecruitment(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeeRejoin(Request $request) { return $this->view('employee-rejoin'); }
    public function postEmployeeRejoin(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeeTermination(Request $request) { return $this->view('employee-termination'); }
    public function postEmployeeTermination(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeeServices(Request $request) { return $this->view('employee-services'); }
    public function postEmployeeServices(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function employeeAppointment(Request $request) { return $this->view('employee-appointment'); }
    public function postEmployeeAppointment(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function maleFemaleRatio(Request $request) { return $this->view('male-female-ratio'); }
    public function postMaleFemaleRatio(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    public function headCount(Request $request) { return $this->view('head-count'); }
    public function postHeadCount(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'a4','landscape'); }

    private function view($viewName)
    {
        $branches = Branch::where('status', 1)->get();
        $departments = Department::where('status', 1)->get();
        $payrollGroup = PayrollGroup::where('status',1)->get();
        $employees = Employee::get()->map(function ($emp) {
                // Format: "employee_code, epf_no - Name" if code != epf_no, else "employee_code - Name"
                $name = trim($emp->first_name . ' ' . $emp->middle_name . ' ' . $emp->last_name);
                if ($emp->employee_code !== $emp->epf_no && !empty($emp->epf_no)) {
                    $emp->display_text = $emp->employee_code . ', ' . $emp->epf_no . ' - ' . $name;
                } else {
                    $emp->display_text = $emp->employee_code . ' - ' . $name;
                }
                // For search: include code, epf_no, and name
                $emp->search_text = $emp->employee_code . ' ' . $emp->epf_no . ' ' . $name;
                return $emp;
            });
        $paymentMethods = PaymentMethod::where('status',1)->get();
        $reportName = $viewName;
        $reportTitle = ucwords(str_replace('-', ' ', $viewName));
        return view('hr_reports::masters.' . $viewName, compact('branches', 'departments', 'reportName', 'reportTitle','payrollGroup','employees','paymentMethods'));
    }

    private function generate(Request $request, ReportManager $manager , $size,$layout)
    {
        return $manager->generate(
            $request->report_name,
            $request->all(),
            $request->format,
            $size,
            $layout
        );
    }

}
