<?php

namespace Tests\Feature;

use App\Models\Certificate;
use App\Models\CertificateTemplate;
use App\Models\College;
use App\Models\Course;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CertificateVerificationTest extends TestCase
{
    use RefreshDatabase;

    public function test_public_verification_page_displays_active_certificate(): void
    {
        $college = College::create(['name' => 'Delhi University']);
        $student = Student::create(['full_name' => 'Test Student', 'college_id' => $college->id]);
        $course = Course::create(['title' => 'Web Development']);
        $template = CertificateTemplate::create([
            'title' => 'Internship Certificate',
            'slug' => 'internship-certificate',
            'view_name' => 'pdf.templates.internship',
        ]);

        $certificate = Certificate::create([
            'certificate_uuid' => 'HWS-2026-TEST01',
            'student_id' => $student->id,
            'template_id' => $template->id,
            'course_id' => $course->id,
            'start_date' => '2026-01-01',
            'end_date' => '2026-03-31',
            'duration' => '3 Months',
            'issue_date' => '2026-04-01',
            'status' => 'active',
        ]);

        $response = $this->get('/verify/HWS-2026-TEST01');

        $response->assertStatus(200);
        $response->assertSee('Test Student');
        $response->assertSee('Delhi University');
        $response->assertSee('Web Development');
        $response->assertSee('Official & Authentic Certificate', false);
    }

    public function test_public_verification_shows_revoked_status_for_cancelled_certificate(): void
    {
        $college = College::create(['name' => 'Jamia Millia']);
        $student = Student::create(['full_name' => 'Revoked Student', 'college_id' => $college->id]);
        $template = CertificateTemplate::create([
            'title' => 'Internship Certificate',
            'slug' => 'internship-certificate',
            'view_name' => 'pdf.templates.internship',
        ]);

        $certificate = Certificate::create([
            'certificate_uuid' => 'HWS-2026-REVOKED01',
            'student_id' => $student->id,
            'template_id' => $template->id,
            'start_date' => '2026-01-01',
            'end_date' => '2026-03-31',
            'duration' => '3 Months',
            'issue_date' => '2026-04-01',
            'status' => 'revoked',
            'revocation_reason' => 'Issued in error',
        ]);

        $response = $this->get('/verify/HWS-2026-REVOKED01');

        $response->assertStatus(200);
        $response->assertSee('CERTIFICATE REVOKED');
        $response->assertSee('Issued in error');
    }
}
