<?php

namespace Tests\Feature;

use App\Models\Package;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class OpticalPlatformTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        File::put(storage_path('installed'), '{"installed": true}');

        Package::create([
            'name' => 'Starter 3 Months',
            'months' => 3,
            'price' => 1499,
            'payable_charge' => 1499,
            'description' => 'Starter Plan',
            'is_active' => true,
        ]);
    }

    public function test_landing_page_renders_successfully(): void
    {
        $response = $this->get('/');
        $response->assertStatus(200);
        $response->assertSee('MyOpticals');
    }

    public function test_customer_can_browse_frames_api(): void
    {
        $response = $this->getJson('/api/v1/customer/frames');
        $response->assertStatus(200);
        $response->assertJsonStructure(['success', 'data']);
    }

    public function test_vendor_can_register_and_receive_token(): void
    {
        $response = $this->postJson('/api/v1/auth/register', [
            'name' => 'Dr. Mehta',
            'email' => 'mehta@opticals.com',
            'phone' => '9898989898',
            'password' => 'Password@123',
            'role' => 'vendor',
            'vendor_name' => 'Mehta Eye Care',
        ]);

        $response->assertStatus(201);
        $response->assertJson(['success' => true]);
        $this->assertDatabaseHas('users', ['email' => 'mehta@opticals.com']);
    }

    public function test_superadmin_can_access_login_page(): void
    {
        $response = $this->get('/admin/login');
        $response->assertStatus(200);
        $response->assertSee('SuperAdmin');
    }
}
