<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SiteAdminProtectedPagesTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsSiteAdmin(): void
    {
        $this->seed();

        $this->post('/site-admin/login', [
            'email' => 'merchant.admin@demo.local',
            'password' => 'Merchant@123456',
        ])->assertRedirect('/site-admin');
    }

    public function test_guest_is_redirected_to_site_admin_login_for_protected_pages(): void
    {
        foreach (['/site-admin', '/site-admin/merchants', '/site-admin/merchants/export', '/site-admin/products', '/site-admin/products/export', '/site-admin/orders', '/site-admin/orders/export'] as $path) {
            $this->get($path)->assertRedirect('/site-admin/login');
        }
    }

    public function test_site_admin_can_access_key_site_pages(): void
    {
        $this->loginAsSiteAdmin();

        $this->get('/site-admin')->assertOk()->assertSee('站点后台仪表盘');
        $this->get('/site-admin/merchants')->assertOk()->assertSee('站点商家');
        $this->get('/site-admin/merchants/export')->assertOk()->assertHeader('content-type', 'text/csv; charset=UTF-8');
        $this->get('/site-admin/products')->assertOk()->assertSee('站点商品');
        $this->get('/site-admin/products/export')->assertOk()->assertHeader('content-type', 'text/csv; charset=UTF-8');
        $this->get('/site-admin/orders')->assertOk()->assertSee('站点订单');
        $this->get('/site-admin/orders/export')->assertOk()->assertHeader('content-type', 'text/csv; charset=UTF-8');
    }

    public function test_platform_admin_is_forbidden_from_site_admin_pages(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');

        $this->get('/site-admin')->assertForbidden()->assertSee('当前账号未绑定站点后台访问权限');
    }
}
