<?php

namespace Tests;

use App\Models\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Testing\TestResponse;

abstract class TestCase extends BaseTestCase
{
    protected function admin(array $attributes = []): User
    {
        return User::factory()->admin()->create($attributes);
    }

    protected function customer(array $attributes = []): User
    {
        return User::factory()->create($attributes);
    }

    /**
     * Laravel's test client does not automatically carry cookies between
     * separate get()/post() calls — each is an independent simulated
     * request. Thread a guest session cookie through explicitly when a
     * test genuinely needs cross-request continuity for an
     * unauthenticated user (guest cart, guest checkout). Mutates the test
     * case's default cookie jar in place, so every call afterward keeps
     * carrying it automatically.
     */
    protected function carryGuestSession(TestResponse $response): void
    {
        $cookieName = config('session.cookie');

        foreach ($response->headers->getCookies() as $cookie) {
            if ($cookie->getName() === $cookieName) {
                $this->withCookie($cookieName, $cookie->getValue());

                return;
            }
        }
    }
}
