<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use function PHPUnit\Framework\assertStringContainsString;
use function PHPUnit\Framework\assertStringNotContainsString;

class ExportEndpointsTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

    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_admin_order_and_product_exports_return_csv_response(): void
    {
        $this->loginAsPlatformAdmin();

        $this->get('/admin/orders/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $this->get('/admin/products/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $this->get('/admin/products/import-histories/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');
    }

    public function test_admin_product_export_uses_chinese_detail_headers(): void
    {
        $this->loginAsPlatformAdmin();

        $response = $this->get('/admin/products/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,站点ID,站点名称,分类ID,分类名称,分类标识,商品标题,商品Slug,SKU,售价,划线价,库存,状态,商品简介,创建时间,更新时间', $content);
    }

    public function test_merchant_order_and_product_exports_return_csv_response(): void
    {
        $this->loginAsMerchantAdmin();

        $this->get('/merchant-admin/orders/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $this->get('/merchant-admin/products/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');

        $this->get('/merchant-admin/products/import-histories/export')
            ->assertOk()
            ->assertHeader('content-type', 'text/csv; charset=UTF-8');
    }

    public function test_merchant_product_export_uses_chinese_detail_headers(): void
    {
        $this->loginAsMerchantAdmin();

        $response = $this->get('/merchant-admin/products/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,分类ID,分类名称,分类标识,商品标题,商品Slug,SKU,售价,划线价,库存,状态,商品简介,创建时间,更新时间', $content);
    }

    public function test_admin_order_export_uses_chinese_detail_headers(): void
    {
        $this->loginAsPlatformAdmin();

        $response = $this->get('/admin/orders/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,商家ID,商家名称,用户ID,订单号,订单状态,支付状态,平台,设备类型,支付渠道,买家姓名,买家手机,买家邮箱,商品金额,优惠金额,运费,实付金额,商品行数,商品件数,商品摘要,创建时间,支付时间,发货时间,完成时间,备注', $content);
        assertStringContainsString('设备类型,全部', $content);
        assertStringContainsString('支付渠道,全部', $content);
        assertStringContainsString('桌面浏览器,微信支付', $content);
        assertStringContainsString('移动浏览器,支付宝', $content);
        assertStringContainsString('小程序环境,微信支付', $content);
        assertStringContainsString('微信内网页,微信支付', $content);
        assertStringContainsString('"APP 接口预留","APP 接口",微信支付', $content);
        assertStringNotContainsString('ID,merchant_id,merchant_name', $content);
        assertStringNotContainsString(',desktop,wechat_pay,', $content);
        assertStringNotContainsString(',mobile,alipay,', $content);
        assertStringNotContainsString(',mini-program,wechat_pay,', $content);
    }

    public function test_merchant_order_export_uses_chinese_detail_headers(): void
    {
        $this->loginAsMerchantAdmin();

        $response = $this->get('/merchant-admin/orders/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,用户ID,订单号,订单状态,支付状态,平台,设备类型,支付渠道,买家姓名,买家手机,买家邮箱,商品金额,优惠金额,运费,实付金额,商品行数,商品件数,商品摘要,创建时间,支付时间,发货时间,完成时间,备注', $content);
        assertStringContainsString('设备类型,全部', $content);
        assertStringContainsString('支付渠道,全部', $content);
        assertStringContainsString('桌面浏览器,微信支付', $content);
        assertStringContainsString('移动浏览器,支付宝', $content);
        assertStringContainsString('小程序环境,微信支付', $content);
        assertStringContainsString('微信内网页,微信支付', $content);
        assertStringContainsString('"APP 接口预留","APP 接口",微信支付', $content);
        assertStringNotContainsString('ID,user_id,order_no,status,payment_status,platform', $content);
        assertStringNotContainsString(',desktop,wechat_pay,', $content);
        assertStringNotContainsString(',mobile,alipay,', $content);
        assertStringNotContainsString(',mini-program,wechat_pay,', $content);
    }

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

        $response = $this->get('/site-admin/products/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,分类ID,分类名称,商品标题,商品Slug,SKU,售价,划线价,库存,状态,商品简介,创建时间,更新时间', $content);
        assertStringNotContainsString('ID,category_id,category_name,title,slug,sku,price,original_price,stock,status,summary,created_at,updated_at', $content);
    }

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

        $response = $this->get('/site-admin/orders/export');
        $response->assertOk();
        $content = $response->streamedContent();

        assertStringContainsString('ID,订单号,订单状态,支付状态,平台,设备类型,支付渠道,买家姓名,买家手机,买家邮箱,商品金额,优惠金额,运费,实付金额,创建时间,支付时间,完成时间,备注', $content);
        assertStringContainsString('设备类型,全部', $content);
        assertStringContainsString('支付渠道,全部', $content);
        assertStringContainsString('桌面浏览器,微信支付', $content);
        assertStringContainsString('移动浏览器,支付宝', $content);
        assertStringContainsString('小程序环境,微信支付', $content);
        assertStringContainsString('微信内网页,微信支付', $content);
        assertStringContainsString('"APP 接口预留","APP 接口",微信支付', $content);
        assertStringNotContainsString('ID,order_no,status,payment_status,platform,device_type,payment_channel', $content);
        assertStringNotContainsString(',desktop,wechat_pay,', $content);
        assertStringNotContainsString(',mobile,alipay,', $content);
        assertStringNotContainsString(',mini-program,wechat_pay,', $content);
    }
}
