<?php

namespace App\Modules\Company\Loan\Billings;

use App\Http\Resources\Company\LoanBillingBorrowedEmployees as LoanBillingBorrowedEmployeesResource;
use App\Http\Resources\Company\LoanBillingClientInvoice as LoanBillingClientInvoiceResource;
use App\Models\LoanBorrowList;
use App\Traits\SearchTraits;
use App\Traits\SortingTraits;
use App\Traits\UserTraits;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\InvoicesExport;

class Invoices
{
    use SortingTraits, SearchTraits, UserTraits;

    public function borrowedEmployees($payload)
    {
        // sorting
        $sortField = $this->sortField($payload, 'first_name'); // first_name = default column to search
        $sortOrder = $this->sortOrder($payload, 'asc');

        $companyId = $this->getCurrentUser()->company_id;
        return LoanBillingBorrowedEmployeesResource::collection(
            LoanBorrowList::select(
                'workers.id',
                'workers.worker_id',
                'workers.first_name',
                'workers.last_name')
                ->join('workers', 'workers.id', '=', 'loan_borrow_lists.worker_id')
                ->where('loan_borrow_lists.status', '!=', 'Approved')
                ->where('loan_borrow_lists.loaner_company_id', $companyId)
                ->groupBy('loan_borrow_lists.borrower_company_id')
                ->groupBy('loan_borrow_lists.worker_id')
                ->orderBy($sortField, $sortOrder)
                ->get()
        )->response()->setStatusCode(201);
    }

    public function getData($payload)
    {
        // sorting
        $sortField = $this->sortField($payload, 'initial_date'); // initial_date = default column to search
        $sortOrder = $this->sortOrder($payload);

        // searching
        $nameSearch = $this->searchField($payload->first_name);
        $workerCompanyIdSearch = $this->searchField($payload->worker_company_id);

        $borrowed_worker_id = $payload->borrowed_worker_id;
        $companyId = $this->getCurrentUser()->company_id;
        return LoanBillingClientInvoiceResource::collection(
            LoanBorrowList::select(
                'loan_borrow_lists.id',
                'loan_borrow_lists.rate',
                'loan_borrow_lists.original_rate',
                'loan_borrow_lists.status',
                'workers.first_name',
                'workers.last_name',
                'workers.worker_id as worker_company_id',
                'IBR.initial_date',
                'IBR.cutoff_date',
                'IBR.days',
                'IBR.other_expense')
                ->join('inbound_billing_requests AS IBR', 'IBR.loan_borrow_lists_id', '=', 'loan_borrow_lists.id')
                ->join('workers', 'workers.id', '=', 'loan_borrow_lists.worker_id')
                ->where('loan_borrow_lists.status', '!=', 'Approved')
                ->where('loan_borrow_lists.status', '!=', 'Pending')
                ->where('loan_borrow_lists.loaner_company_id', $companyId)
                ->where('loan_borrow_lists.worker_id', $borrowed_worker_id)
                ->when(!empty($nameSearch), function ($query) use ($nameSearch) {
                    return $query->where(function ($query) use ($nameSearch) {
                        $query->where('workers.first_name', 'LIKE', $nameSearch . '%')
                            ->orWhere('workers.last_name', 'LIKE', $nameSearch . '%')
                            ->orWhere(DB::raw('CONCAT(workers.first_name, " ", workers.last_name)'), 'LIKE', $nameSearch . '%');
                    });
                })
                ->when(!empty($workerCompanyIdSearch), function ($query) use ($workerCompanyIdSearch) {
                    return $query->where(function ($query) use ($workerCompanyIdSearch) {
                        $query->where('workers.worker_id', 'LIKE', $workerCompanyIdSearch . '%');
                    });
                })
                ->orderBy($sortField, $sortOrder)
                ->paginate(20)
        )->response()->setStatusCode(201);
    }

    public function exportLoanInvoice($payload)
    {
        $filters = $payload->all();
        return Excel::download(new InvoicesExport($this->getCurrentUser()->company_id, $filters), 'Invoices.csv');
    }
}
