<?php

namespace Tests\Feature;

use App\Domains\Identity\Actions\RegisterTenantAction;
use App\Domains\Orders\Models\Order;
use App\Domains\Tenancy\Models\Tenant;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\PlanSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class OrderDocumentTest extends TestCase
{
    use RefreshDatabase;

    public function test_owner_can_open_printable_order_page(): void
    {
        $this->seed([PlanSeeder::class, PermissionSeeder::class]);

        /** @var User $user */
        $user = app(RegisterTenantAction::class)->execute([
            'company_name' => 'Docs Demo Co',
            'full_name' => 'Docs Owner',
            'email' => 'docs@example.com',
            'password' => 'Password123!',
            'country' => 'LB',
            'phone' => '+96170000011',
        ]);

        Tenant::query()->findOrFail($user->tenant_id)->update([
            'onboarding_completed_at' => now(),
        ]);

        /** @var Order $order */
        $order = Order::query()->where('tenant_id', $user->tenant_id)->firstOrFail();

        $this->actingAs($user)
            ->get(route('orders.print', $order))
            ->assertOk()
            ->assertSee($order->order_number);
    }

    public function test_owner_can_download_order_pdf(): void
    {
        $this->seed([PlanSeeder::class, PermissionSeeder::class]);

        /** @var User $user */
        $user = app(RegisterTenantAction::class)->execute([
            'company_name' => 'Docs Demo Co',
            'full_name' => 'Docs Owner',
            'email' => 'docs@example.com',
            'password' => 'Password123!',
            'country' => 'LB',
            'phone' => '+96170000011',
        ]);

        Tenant::query()->findOrFail($user->tenant_id)->update([
            'onboarding_completed_at' => now(),
        ]);

        /** @var Order $order */
        $order = Order::query()->where('tenant_id', $user->tenant_id)->firstOrFail();

        $response = $this->actingAs($user)
            ->get(route('orders.pdf', $order));

        $response->assertOk();
        $response->assertHeader('content-type', 'application/pdf');
    }
}
