<?php

namespace App\Http\Controllers;

use App\Models\RejectionReturnNote;
use App\Http\Requests\RejectionReturnNoteRequest;
use App\Models\GoodsReceiving;
use App\Models\GrnItemBatch;
use App\Models\StoreIssueRequisition;
use App\Services\StoreIssueRequisitionService;
use App\Services\RejectionReturnNoteService;
use App\Services\GoodsReceivingService;
use App\Services\CompanyService;
use App\Services\InvoiceService;


class RejectionReturnNoteController extends Controller
{
    protected $storeIssueRequisitionService;
    protected $rejectionReturnNoteService;
    protected $goodsReceivingService;
    protected $invoiceService;
    protected $companyService;

    public function __construct(RejectionReturnNoteService $rejectionReturnNoteService, InvoiceService $invoiceService, CompanyService $companyService, GoodsReceivingService $goodsReceivingService, StoreIssueRequisitionService $storeIssueRequisitionService)
    {
        $this->storeIssueRequisitionService = $storeIssueRequisitionService;
        $this->rejectionReturnNoteService = $rejectionReturnNoteService;
        $this->goodsReceivingService = $goodsReceivingService;
        $this->invoiceService = $invoiceService;
        $this->companyService = $companyService;
    }

    public function index()
    {
        if (!auth()->user()->hasPermission('Rejection Return Note List')) {
            abort(403, 'Unauthorized action.');
        }
        $rejectionReturnNotes = $this->rejectionReturnNoteService->getAll();
        return view('admin.rejection-return-notes.index', compact('rejectionReturnNotes'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        if (!auth()->user()->hasPermission('Rejection Return Note Create')) {
            abort(403, 'Unauthorized action.');
        }
        if (auth()->user()->hasRole('Superadmin')) {
            $newRejectionCode = "RRN";
        } else {
            $newRejectionCode = $this->rejectionReturnNoteService->createCode(auth()->user()->employee->company_id);
        }
        $companies = $this->companyService->getAllCompanies();
        $invoices = $this->invoiceService->getAllInvoices('purchase');
        $goodsReceivings = $this->goodsReceivingService->getAllGoodsReceivings();
        $requisitions = $this->storeIssueRequisitionService->getAll();
        return view('admin.rejection-return-notes.create', compact('companies', 'invoices', 'goodsReceivings', 'requisitions', 'newRejectionCode'));
    }

    public function productionCreate()
    {
        if (!auth()->user()->hasPermission('Rejection Return Note Create')) {
            abort(403, 'Unauthorized action.');
        }
        if (auth()->user()->hasRole('Superadmin')) {
            $newRejectionCode = "RRN";
        } else {
            $newRejectionCode = $this->rejectionReturnNoteService->createCode(auth()->user()->employee->company_id);
        }
        $companies = $this->companyService->getAllCompanies();
        $invoices = $this->invoiceService->getAllInvoices('purchase');
        $goodsReceivings = $this->goodsReceivingService->getAllGoodsReceivings();
        $requisitions = $this->storeIssueRequisitionService->getAll();
        return view('admin.rejection-return-notes.production-create', compact('companies', 'invoices', 'goodsReceivings', 'requisitions', 'newRejectionCode'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(RejectionReturnNoteRequest $request)
    {
        // dd($request->all());
        if (!auth()->user()->hasPermission('Rejection Return Note Create')) {
            abort(403, 'Unauthorized action.');
        }
        $this->rejectionReturnNoteService->create($request->validated());
        return redirect()->route('rejection-return-notes.index')->with('success', 'Rejection Return Note created successfully.');
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        if (!auth()->user()->hasPermission('Rejection Return Note View')) {
            abort(403, 'Unauthorized action.');
        }
        $rejectionReturnNote = $this->rejectionReturnNoteService->getById($id);
        $previous = RejectionReturnNote::where('id', '<', $rejectionReturnNote->id)
            ->orderBy('id', 'desc')
            ->first();

        $next = RejectionReturnNote::where('id', '>', $rejectionReturnNote->id)
            ->orderBy('id', 'asc')
            ->first();

        // Get all purchase orders for the dropdown (ordered by creation date, newest first)
        $allRRNs = RejectionReturnNote::orderBy('created_at', 'desc')
            ->select('id', 'code', 'created_at')
            ->get();
        return view('admin.rejection-return-notes.show', compact('rejectionReturnNote', 'previous', 'next', 'allRRNs'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        if (!auth()->user()->hasPermission('Rejection Return Note Update')) {
            abort(403, 'Unauthorized action.');
        }
        $rejectionReturnNote = $this->rejectionReturnNoteService->getById($id);
        $companies = $this->companyService->getAllCompanies();
        $invoices = $this->invoiceService->getAllInvoices('purchase');
        $goodsReceivings = $this->goodsReceivingService->getAllGoodsReceivings();
        // $requisitions = $this->storeIssueRequisitionService->getAll();
        $grnIds = GoodsReceiving::where('invoice_id', $rejectionReturnNote->invoice_id)->pluck('id')->toArray();
        $grnItemBatchIds = GrnItemBatch::whereIn('grn_id', $grnIds)->pluck('id')->toArray();

        // Step 2: get SIRs that used these items
        $requisitions = StoreIssueRequisition::where('is_production', ($rejectionReturnNote->is_production == '1' || $rejectionReturnNote->is_production == 1 ? '1' : '0'))
            ->whereHas('items', function ($q) use ($grnItemBatchIds) {
                $q->whereIn('batch_id', $grnItemBatchIds);
            })
            ->orderBy('code', 'asc')
            ->get();

        if ($rejectionReturnNote->is_production == '1' || $rejectionReturnNote->is_production == 1) {
            return view('admin.rejection-return-notes.production-edit', compact('rejectionReturnNote', 'companies', 'invoices', 'goodsReceivings', 'requisitions'));
        } else {
            return view('admin.rejection-return-notes.edit', compact('rejectionReturnNote', 'companies', 'invoices', 'goodsReceivings', 'requisitions'));
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(RejectionReturnNoteRequest $request, RejectionReturnNote $rejectionReturnNote)
    {
        if (!auth()->user()->hasPermission('Rejection Return Note Update')) {
            abort(403, 'Unauthorized action.');
        }
        $this->rejectionReturnNoteService->update($rejectionReturnNote, $request->validated());
        return redirect()->route('rejection-return-notes.index')->with('success', 'Rejection Return Note updated successfully');
    }

    public function destroy(RejectionReturnNote $rejectionReturnNote)
    {
        if (!auth()->user()->hasPermission('Rejection Return Note Delete')) {
            abort(403, 'Unauthorized action.');
        }
        $this->rejectionReturnNoteService->delete($rejectionReturnNote);
        return redirect()->route('rejection-return-notes.index')->with('success', 'Rejection Return Note deleted successfully');
    }

    public function createCodeByCompany($companyId)
    {
        $rejection = $this->rejectionReturnNoteService->createCode($companyId);
        return response()->json(['rejection' => $rejection]);
    }
}
