<?php

namespace Tests\Feature;

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

class AdminExportTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test guest cannot access export routes.
     */
    public function test_guest_cannot_access_export_routes(): void
    {
        $responseCsv = $this->get(route('admin.aduan.export'));
        $responseCsv->assertRedirect(route('admin.login'));

        $responsePrint = $this->get(route('admin.aduan.export.excel'));
        $responsePrint->assertRedirect(route('admin.login'));
    }

    /**
     * Test non-admin user cannot access export routes.
     */
    public function test_non_admin_cannot_access_export_routes(): void
    {
        $user = User::factory()->create([
            'role' => 'user',
        ]);

        $responseCsv = $this->actingAs($user)->get(route('admin.aduan.export'));
        $responseCsv->assertRedirect(route('admin.login'));

        $responsePrint = $this->actingAs($user)->get(route('admin.aduan.export.excel'));
        $responsePrint->assertRedirect(route('admin.login'));
    }

    /**
     * Test admin can export CSV and filters are applied.
     */
    public function test_admin_can_export_csv(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        // Create some sample aduans
        Aduan::create([
            'nama_pelapor' => 'Budi Santoso',
            'desa' => 'wonosalam',
            'kategori' => 'keamanan_ketertiban',
            'prioritas' => 'penting',
            'status' => 'menunggu',
            'isi_aduan' => 'Pencurian sepeda motor di desa Wonosalam.',
        ]);

        Aduan::create([
            'nama_pelapor' => 'Siti Aminah',
            'desa' => 'botorejo',
            'kategori' => 'kebersihan_lingkungan',
            'prioritas' => 'biasa',
            'status' => 'selesai',
            'isi_aduan' => 'Tumpukan sampah menyumbat selokan desa Botorejo.',
        ]);

        // Request CSV export
        $response = $this->actingAs($admin)->get(route('admin.aduan.export'));

        $response->assertStatus(200);
        $response->assertHeader('Content-Type', 'text/csv; charset=UTF-8');
        $response->assertHeader('Content-Disposition', 'attachment; filename="laporan-aduan-silapor-' . now()->format('Y-m-d') . '.csv"');

        // Capture streamed response content
        ob_start();
        $response->sendContent();
        $content = ob_get_clean();

        $this->assertStringContainsString('Budi Santoso', $content);
        $this->assertStringContainsString('Siti Aminah', $content);

        // Test filtering by village
        $responseFiltered = $this->actingAs($admin)->get(route('admin.aduan.export', ['desa' => 'wonosalam']));
        ob_start();
        $responseFiltered->sendContent();
        $filteredContent = ob_get_clean();

        $this->assertStringContainsString('Budi Santoso', $filteredContent);
        $this->assertStringNotContainsString('Siti Aminah', $filteredContent);
    }

    /**
     * Test admin can export print view.
     */
    public function test_admin_can_export_print(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        // Create some sample aduans
        Aduan::create([
            'nama_pelapor' => 'Budi Santoso',
            'desa' => 'wonosalam',
            'status' => 'menunggu',
            'isi_aduan' => 'Pencurian sepeda motor di desa Wonosalam.',
        ]);

        $response = $this->actingAs($admin)->get(route('admin.aduan.export.excel'));

        $response->assertStatus(200);
        $response->assertViewIs('admin.export_print');
        $response->assertSee('Budi Santoso');
        $response->assertSee('LAPORAN DATA PENGADUAN MASYARAKAT');
    }
}
