<?php

declare(strict_types=1);

namespace App\Modules\Purchases\Application\Services;

use App\Core\Request;

final class PurchasesRouteContext
{
    public function isPurchasesRoute(): bool
    {
        return str_starts_with(Request::path(), '/purchases/');
    }

    public function dashboardCrumb(): array
    {
        return ['label' => 'لوحة التحكم', 'url' => url('/admin/dashboard')];
    }

    public function purchasesBoardPath(): string
    {
        return url('/purchases/invoices');
    }

    public function invoicesPath(bool $forReturn = false): string
    {
        $base = url('/purchases/invoices');
        return $forReturn ? $base . '?for_return=1' : $base;
    }

    public function invoiceFormPath(int $id = 0): string
    {
        $base = url('/purchases/invoices/form');
        return $id > 0 ? $base . '?id=' . $id : $base;
    }

    public function invoiceViewPath(int $id = 0): string
    {
        $base = url('/purchases/invoices/view');
        return $id > 0 ? $base . '?id=' . $id : $base;
    }

    public function returnsPath(): string
    {
        return url('/purchases/returns');
    }

    public function returnFormPath(int $id = 0, int $invoiceId = 0): string
    {
        $base = url('/purchases/returns/form');
        $params = [];
        if ($id > 0) {
            $params[] = 'id=' . $id;
        }
        if ($invoiceId > 0) {
            $params[] = 'invoice_id=' . $invoiceId;
        }
        return $params ? $base . '?' . implode('&', $params) : $base;
    }

    public function returnViewPath(int $id = 0): string
    {
        $base = url('/purchases/returns/view');
        return $id > 0 ? $base . '?id=' . $id : $base;
    }

    public function supplierPaymentsPath(): string
    {
        return url('/purchases/supplier-payments');
    }

    public function supplierPaymentFormPath(int $id = 0, int $partyId = 0): string
    {
        $base = url('/purchases/supplier-payments/form');
        $params = [];
        if ($id > 0) {
            $params[] = 'id=' . $id;
        }
        if ($partyId > 0) {
            $params[] = 'party_id=' . $partyId;
        }
        return $params ? $base . '?' . implode('&', $params) : $base;
    }

    public function supplierPaymentViewPath(int $id = 0): string
    {
        $base = url('/purchases/supplier-payments/view');
        return $id > 0 ? $base . '?id=' . $id : $base;
    }

    public function supplierStatementPath(int $partyId = 0): string
    {
        $base = url('/purchases/supplier-payments/statement');
        return $partyId > 0 ? $base . '?party_id=' . $partyId : $base;
    }

    public function supplierAgingPath(int $partyId = 0): string
    {
        $base = url('/purchases/supplier-payments/aging');
        return $partyId > 0 ? $base . '?party_id=' . $partyId : $base;
    }
}
