<?php

namespace Tests\Feature;

use App\Models\Article;
use App\Models\Attachment;
use App\Models\Comment;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class SablogImportTest extends TestCase
{
    use RefreshDatabase;

    protected string $fixtureDb;

    protected function setUp(): void
    {
        parent::setUp();

        Storage::fake('attachments');
        Config::set('filesystems.disks.attachments', [
            'driver' => 'local',
            'root' => Storage::disk('attachments')->path(''),
            'throw' => true,
        ]);

        $this->fixtureDb = storage_path('framework/testing/sablog-fixture.sqlite');
        @mkdir(dirname($this->fixtureDb), 0777, true);
        if (is_file($this->fixtureDb)) {
            unlink($this->fixtureDb);
        }

        $pdo = new \PDO('sqlite:'.$this->fixtureDb);
        $pdo->exec(file_get_contents(base_path('tests/fixtures/sablog/schema.sql')));
        $pdo->exec(file_get_contents(base_path('tests/fixtures/sablog/seed.sql')));

        Config::set('database.connections.sablog', [
            'driver' => 'sqlite',
            'database' => $this->fixtureDb,
            'prefix' => '',
            'foreign_key_constraints' => true,
        ]);
        DB::purge('sablog');
    }

    protected function tearDown(): void
    {
        if (isset($this->fixtureDb) && is_file($this->fixtureDb)) {
            unlink($this->fixtureDb);
        }

        parent::tearDown();
    }

    public function test_import_raw_preserves_ids_and_skips_legacy_tables(): void
    {
        $this->artisan('sablog:import', [
            '--connection' => 'sablog',
            '--mode' => 'raw',
            '--attachments' => base_path('tests/fixtures/sablog/attachments'),
            '--disk' => 'attachments',
        ])->assertSuccessful();

        $this->assertDatabaseHas('articles', ['id' => 10, 'title' => 'Fixture Post', 'content_format' => 'html']);
        $this->assertDatabaseHas('categories', ['id' => 1, 'name' => '随笔']);
        $this->assertDatabaseHas('comments', ['id' => 5, 'moderation_status' => Comment::STATUS_APPROVED]);
        $this->assertDatabaseHas('users', ['id' => 1, 'username' => 'admin']);

        $article = Article::query()->findOrFail(10);
        $this->assertStringContainsString('[attach=1]', $article->content);

        $attachment = Attachment::query()->findOrFail(1);
        $this->assertNotNull($attachment->synced_at);
        $this->assertTrue(Storage::disk('attachments')->exists($attachment->path));

        $user = User::query()->findOrFail(1);
        $this->assertSame(md5('password'), $user->password_legacy);
    }

    public function test_import_markdown_rewrites_attach_embeds(): void
    {
        $this->artisan('sablog:import', [
            '--connection' => 'sablog',
            '--mode' => 'markdown',
            '--attachments' => base_path('tests/fixtures/sablog/attachments'),
            '--disk' => 'attachments',
        ])->assertSuccessful();

        $article = Article::query()->findOrFail(10);
        $this->assertSame('markdown', $article->content_format);
        $this->assertStringContainsString('attach:1', $article->content);
        $this->assertStringNotContainsString('[attach=1]', $article->content);
    }
}
