<?php

namespace App\Livewire\Reports;

use App\Models\Party;
use App\Models\Account;
use App\Models\Ledger;
use App\Models\Page;
use Livewire\Component;
use Livewire\Attributes\Layout;

#[Layout('layouts.app')]
class AccountStatementReport extends Component
{
    public $party_id = '';
    public $account_id = '';
    public $ledger_id = '';
    public $page_id = '';

    public $date_from = '';
    public $date_to = '';
    
    public $with_opening_balance = true;
    public $with_notes = false;
    public $hide_credit_party = false;
    public $hide_transaction_number = true;
    public $hide_reference = true;

    public function updatedPartyId()
    {
        $this->account_id = '';
        $this->ledger_id = '';
        $this->page_id = '';
    }

    public function updatedAccountId()
    {
        $this->ledger_id = '';
        $this->page_id = '';
    }

    public function updatedLedgerId()
    {
        $this->page_id = '';
    }

    public function generateReport()
    {
        if (!$this->page_id) {
            $this->addError('page_id', 'يجب اختيار الصفحة لتوليد التقرير.');
            return;
        }

        $url = route('pages.report', ['page' => $this->page_id]) . '?' . http_build_query([
            'filter' => 1,
            'date_from' => $this->date_from,
            'date_to' => $this->date_to,
            'with_opening_balance' => $this->with_opening_balance ? 1 : null,
            'with_notes' => $this->with_notes ? 1 : null,
            'hide_credit_party' => $this->hide_credit_party ? 1 : null,
            'hide_transaction_number' => $this->hide_transaction_number ? 1 : null,
            'hide_reference' => $this->hide_reference ? 1 : null,
        ]);

        $this->dispatch('open-report', url: $url);
    }

    public function render()
    {
        if (!auth()->user()->can('view records')) {
            abort(403);
        }

        $parties = Party::orderBy('name')->get();
        
        $accountsQuery = Account::orderBy('name');
        if ($this->party_id) {
            $accountsQuery->where('party_id', $this->party_id);
        }
        $accounts = $accountsQuery->get();

        $ledgersQuery = Ledger::orderBy('name');
        if ($this->account_id) {
            $ledgersQuery->where('account_id', $this->account_id);
        } elseif ($this->party_id) {
            $ledgersQuery->whereHas('account', function($q) {
                $q->where('party_id', $this->party_id);
            });
        }
        $ledgers = $ledgersQuery->get();
        
        $pagesQuery = Page::orderBy('code');
        if ($this->ledger_id) {
            $pagesQuery->where('ledger_id', $this->ledger_id);
        } elseif ($this->account_id) {
            $pagesQuery->whereHas('ledger', function($q) {
                $q->where('account_id', $this->account_id);
            });
        } elseif ($this->party_id) {
            $pagesQuery->whereHas('ledger.account', function($q) {
                $q->where('party_id', $this->party_id);
            });
        }
        $pages = $pagesQuery->get();

        return view('livewire.reports.account-statement-report', [
            'parties' => $parties,
            'accounts' => $accounts,
            'ledgers' => $ledgers,
            'pages' => $pages,
        ]);
    }
}
