<?php

namespace Tests\Feature;

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

class MassListingTest extends TestCase
{
    use RefreshDatabase;

    private function actingUser(): User
    {
        // refresh() picks up DB defaults (is_active) the factory doesn't set,
        // which ActiveUserMiddleware reads off the in-memory model.
        return User::factory()->create()->refresh();
    }

    public function test_pasted_text_is_parsed_and_shown_for_review(): void
    {
        $user = $this->actingUser();

        $response = $this->actingAs($user)->post('/mass-listing', [
            'items_text' => "Polyend Tracker - \$275\nbarely used, original box\n\nAkai MPKmini mk2 - \$70",
        ]);

        $response->assertRedirect(route('items.review-mass-listing-quick'));

        $review = $this->actingAs($user)->get(route('items.review-mass-listing-quick'));
        $review->assertOk();
        $review->assertSee('Polyend Tracker');
        $review->assertSee('Akai MPKmini mk2');
        $review->assertSee('2 items');
    }

    public function test_items_can_be_published_live(): void
    {
        $user = $this->actingUser();
        $category = Category::create(['name' => 'Synths', 'slug' => 'synths', 'is_active' => true]);

        $response = $this->actingAs($user)->postJson(route('items.create-all-live'), [
            'status' => 'active',
            'items' => [
                ['title' => 'Polyend Tracker', 'price' => 275, 'condition' => 'Good', 'category_id' => $category->id, 'quantity' => 1, 'description' => 'boxed'],
            ],
        ]);

        $response->assertOk()->assertJson(['success' => true, 'created' => 1]);

        $this->assertDatabaseHas('items', [
            'user_id' => $user->id,
            'title' => 'Polyend Tracker',
            'status' => 'active',
            'category_id' => $category->id,
        ]);
    }

    public function test_items_can_be_saved_as_drafts(): void
    {
        $user = $this->actingUser();

        $response = $this->actingAs($user)->postJson(route('items.create-all-live'), [
            'status' => 'draft',
            'items' => [
                ['title' => 'Akai MPKmini', 'price' => 70, 'condition' => 'Fair'],
            ],
        ]);

        $response->assertOk()->assertJson(['success' => true]);
        $this->assertSame(route('items.my-drafts'), $response->json('redirect'));
        $this->assertDatabaseHas('items', ['title' => 'Akai MPKmini', 'status' => 'draft']);
    }

    public function test_invalid_items_are_rejected(): void
    {
        $user = $this->actingUser();

        // Negative price
        $this->actingAs($user)->postJson(route('items.create-all-live'), [
            'status' => 'active',
            'items' => [['title' => 'Bad', 'price' => -5, 'condition' => 'Good']],
        ])->assertStatus(422);

        // Bogus status (only draft/active allowed)
        $this->actingAs($user)->postJson(route('items.create-all-live'), [
            'status' => 'sold',
            'items' => [['title' => 'Ok', 'price' => 5, 'condition' => 'Good']],
        ])->assertStatus(422);

        // Bogus condition
        $this->actingAs($user)->postJson(route('items.create-all-live'), [
            'status' => 'active',
            'items' => [['title' => 'Ok', 'price' => 5, 'condition' => 'Mint-ish']],
        ])->assertStatus(422);

        $this->assertDatabaseCount('items', 0);
    }

    public function test_unparseable_text_returns_error(): void
    {
        $user = $this->actingUser();

        $response = $this->actingAs($user)->from('/mass-listing')->post('/mass-listing', [
            'items_text' => 'just some words with no prices at all',
        ]);

        $response->assertRedirect('/mass-listing');
        $response->assertSessionHasErrors('items_text');
    }
}
