<?php

namespace Tests\Feature\V3\Damage;

use App\Models\Damage;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class UpdateTest extends TestCase
{
    use WithFaker, RefreshDatabase;

    public function test_unauthenticated_user_cannot_update_damage(): void
    {
        $damage =  Damage::factory()->create();;
        $response = $this->getJson(route('v3.damages.update', [$damage->id]));

        $response->assertStatus(401);
    }

    public function test_user_without_permission_cannot_update_damage(): void
    {
        $user = User::factory()->create();
        $damage = Damage::factory()->create();;

        $response = $this->actingAs($user)->getJson(route('v3.damages.update', [$damage->id]));

        $response->assertForbidden();
    }

    public function test_validation_fails_when_required_fields_are_missing(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();


        $damage = Damage::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.required', ['attribute' => "عنوان"]),
                'unit' => __('validation.required', ['attribute' => 'واحد']),
                'base_price' => __('validation.required', ['attribute' => 'قیمت پایه']),
            ]);
    }

    public function test_title_must_be_unique(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $existingDamage = Damage::factory()->create([
            'title' => 'Existing Title'
        ]);

        $damage = Damage::factory()->create();
        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]),[
            'title' => $existingDamage->title,
            'unit' => 'm',
            'base_price' => 5000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.unique', ['attribute' => "عنوان"]),
            ]);
    }

    public function test_title_and_unit_must_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $damage = Damage::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
            'title' => 12345,
            'unit' => 67890,
            'base_price' => 10000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.string', ['attribute' => "عنوان"]),
                'unit' => __('validation.string', ['attribute' => 'واحد']),
            ]);
    }

    public function test_title_must_not_exceed_255_characters(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $damage = Damage::factory()->create();
        $longTitle = $this->faker->text(500);

        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
            'title' => $longTitle,
            'unit' => 'kg',
            'base_price' => 10000,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'title' => __('validation.max.string',['attribute' => 'عنوان', 'max' => 255]),
            ]);
    }

    public function test_base_price_must_be_integer(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $damage = Damage::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
            'title' => 'Valid Title',
            'unit' => 'kg',
            'base_price' => 'invalid_price',]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'base_price' => __('validation.integer', ['attribute' => 'قیمت پایه']),
            ]);
    }


    public function test_user_with_permission_can_update_damage(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-damage'
            ]))
            ->create();

        $damage = Damage::factory()->create();

        $updateData = [
            'title' => 'Updated Damage',
            'unit' => 'litre',
            'base_price' => 20000,
        ];

        $response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), $updateData);

        $response->assertStatus(200)
            ->assertJson([
                'message' => __('messages.successful'),
            ]);

        $this->assertDatabaseHas('damages', [
            'id' => $damage->id,
            'title' => $updateData['title'],
            'unit' => $updateData['unit'],
            'base_price' => $updateData['base_price'],
        ]);
    }


}
