<?php

namespace Zoomyboy\MedialibraryHelper\Tests;

use Illuminate\Http\File;
use Illuminate\Support\Facades\Gate;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Workbench\App\Models\Post;
use Workbench\App\Models\User;

class TestCase extends BaseTestCase
{

    use WithWorkbench;

    /**
     * Generate a pdf file with a filename and get path.
     */
    protected function pdfFile(?string $filename = null): File
    {
        return $this->getFile('pdf.pdf', $filename ?: 'pdf.pdf');
    }

    protected function jpgFile(?string $filename = null): File
    {
        return $this->getFile('jpg.jpg', $filename ?: 'jpg.jpg');
    }

    protected function pngFile(?string $filename = null): File
    {
        return $this->getFile('png.png', $filename ?: 'png.png');
    }

    protected function getFile(string $location, string $as): File
    {
        $path = __DIR__ . '/stubs/' . $location;
        $to = sys_get_temp_dir() . '/' . $as;
        copy($path, $to);

        return new File($to);
    }

    protected function registerModel(): static
    {
        app()->extend('media-library-helpers', fn ($p) => $p->put('post', Post::class));

        return $this;
    }

    protected function newPost(): Post
    {
        return Post::create(['title' => 'Lorem', 'content' => 'aafff']);
    }

    protected function auth(array $policies = []): self
    {
        $this->be(User::factory()->policies($policies)->create());

        return $this;
    }
}
