<?php

namespace Tests\Feature;

use App\Models\CashDrawerTransaction;
use App\Models\Expense;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExpenseApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_expense_can_be_created_approved_and_counted_in_reports(): void
    {
        ['company' => $company, 'user' => $user, 'branch' => $branch] = $this->createUserWithPermissions([
            'shifts.manage',
            'expenses.view',
            'expenses.manage',
            'expenses.approve',
            'reports.view',
        ]);

        $this->actingAsApi($user, $branch->id)
            ->postJson('/api/v1/shifts/open', [
                'opening_float' => 500,
            ])
            ->assertCreated();

        $categoryResponse = $this->actingAsApi($user, $branch->id)
            ->postJson('/api/v1/expense-categories', [
                'name' => 'Utilities',
                'description' => 'Power and water',
            ]);

        $categoryResponse->assertCreated();
        $categoryId = $categoryResponse->json('data.id');

        $createResponse = $this->actingAsApi($user, $branch->id)
            ->postJson('/api/v1/expenses', [
                'expense_category_id' => $categoryId,
                'amount' => 150,
                'description' => 'Emergency power tokens',
                'payment_method' => 'cash',
                'expense_date' => now()->toDateString(),
            ]);

        $createResponse->assertCreated()
            ->assertJsonPath('data.is_approved', false);

        $expenseId = $createResponse->json('data.id');

        $approveResponse = $this->actingAsApi($user, $branch->id)
            ->postJson("/api/v1/expenses/{$expenseId}/approve", [
                'notes' => 'Approved by manager',
            ]);

        $approveResponse->assertOk()
            ->assertJsonPath('data.is_approved', true);

        $this->assertDatabaseHas('expenses', [
            'id' => $expenseId,
            'is_approved' => 1,
        ]);

        $this->assertDatabaseHas('cash_drawer_transactions', [
            'reference_type' => Expense::class,
            'reference_id' => $expenseId,
            'type' => 'expense',
        ]);

        $drawerEntry = CashDrawerTransaction::query()
            ->where('reference_type', Expense::class)
            ->where('reference_id', $expenseId)
            ->latest('id')
            ->first();

        $this->assertNotNull($drawerEntry);
        $this->assertSame('-150.00', $drawerEntry->amount);

        $categories = $this->actingAsApi($user, $branch->id)
            ->getJson('/api/v1/expense-categories');

        $categories->assertOk()
            ->assertJsonPath('data.0.id', $categoryId);

        $expenses = $this->actingAsApi($user, $branch->id)
            ->getJson('/api/v1/expenses');

        $expenses->assertOk()
            ->assertJsonPath('data.0.id', $expenseId);

        $report = $this->actingAsApi($user, $branch->id)
            ->getJson('/api/v1/reports/daily-sales?refresh=1&date_from='.now()->toDateString().'&date_to='.now()->toDateString());

        $report->assertOk()
            ->assertJsonPath('data.0.total_expenses', '150.0000');
    }
}
