<?php

namespace Tests\Feature;

use App\Models\InspectionCollection;
use App\Models\InspectionWorkspace;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WorkspaceTest extends TestCase
{
    use RefreshDatabase;

    public function test_dashboard_displays_workspaces_and_collection_can_be_moved(): void
    {
        $user = User::factory()->create();
        $source = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Rumah Lama',
            'default_currency' => 'IDR',
        ]);
        $target = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Rumah Baru',
            'default_currency' => 'IDR',
        ]);
        $collection = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $source->id,
            'name' => 'Kamar Mandi',
            'collection_type' => 'room',
            'property_name' => 'Rumah Lama',
            'room_name' => 'Kamar Mandi',
        ]);

        $this->actingAs($user)
            ->get('/dashboard')
            ->assertOk()
            ->assertSee('Rumah Lama')
            ->assertSee('Rumah Baru')
            ->assertSee('Kamar Mandi');

        $this->actingAs($user)
            ->post(route('collections.move'), [
                'collection_id' => $collection->id,
                'workspace_id' => $target->id,
            ])
            ->assertRedirect();

        $this->assertDatabaseHas('inspection_collections', [
            'id' => $collection->id,
            'inspection_workspace_id' => $target->id,
        ]);
    }

    public function test_workspace_page_can_update_and_delete_workspace_and_collection(): void
    {
        $user = User::factory()->create();
        $workspace = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Workspace Awal',
            'description' => 'Deskripsi awal',
            'default_currency' => 'IDR',
        ]);
        $collection = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $workspace->id,
            'name' => 'Kamar Tidur',
            'collection_type' => 'room',
            'property_name' => 'Rumah Awal',
            'room_name' => 'Kamar Tidur',
        ]);

        $this->actingAs($user)
            ->get(route('workspaces.index'))
            ->assertOk()
            ->assertSee('Workspace Awal')
            ->assertSee('Deskripsi awal');

        $this->actingAs($user)
            ->put(route('workspaces.update', $workspace), [
                'name' => 'Workspace Baru',
                'description' => 'Deskripsi baru',
                'default_currency' => 'USD',
            ])
            ->assertRedirect();

        $this->assertDatabaseHas('inspection_workspaces', [
            'id' => $workspace->id,
            'name' => 'Workspace Baru',
            'description' => 'Deskripsi baru',
            'default_currency' => 'USD',
        ]);

        $this->actingAs($user)
            ->delete(route('collections.destroy', $collection))
            ->assertRedirect();

        $this->assertDatabaseMissing('inspection_collections', [
            'id' => $collection->id,
        ]);

        $this->actingAs($user)
            ->delete(route('workspaces.destroy', $workspace))
            ->assertRedirect();

        $this->assertDatabaseMissing('inspection_workspaces', [
            'id' => $workspace->id,
        ]);
    }
}
