<?php

namespace App\Livewire\Forms\Studio\Manage;

use App\Enums\ArticleStatus;
use App\Models\Article;
use App\Traits\Media\WithMediaHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;

class ArticleForm extends Form
{
    use WithMediaHandler;

    public ?Article $article = null;

    public string $title = '';

    public string $excerpt = '';

    public string $content = '';

    public string $status = '1';

    public array $category_ids = [];

    public array $thumbnail = [];

    public function rules(): array
    {
        return [
            'title' => ['required', 'string', 'max:200'],
            'excerpt' => ['required', 'string'],
            'content' => ['required', 'string'],
            'status' => ['required', Rule::in(ArticleStatus::cases())],
            'category_ids' => ['required', 'array', 'min:1'],
            'category_ids.*' => Rule::exists('categories', 'id'),
            'thumbnail' => ['required', 'array'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'title' => 'judul',
            'excerpt' => 'cuplikan',
            'content' => 'konten',
            'category_ids' => 'kategori',
            'category_ids.*' => 'kategori',
        ];
    }

    public function setArticle(Article $article): void
    {
        $this->article = $article;

        $this->title = $article->title;
        $this->excerpt = $article->excerpt;
        $this->content = $article->content;
        $this->status = $article->status->value;
        $this->category_ids = $article->categories->pluck('id')->toArray();
        $this->thumbnail = $this->mapMediaCollection($article->getMedia('thumbnail'));
    }

    public function store(): string
    {
        $this->validate();

        DB::transaction(function () use (&$article) {
            $article = Article::create([
                'author_id' => auth()->id(),
                'title' => $this->title,
                'excerpt' => $this->excerpt,
                'content' => $this->content,
                'status' => $this->status,
                'published_at' => $this->status == ArticleStatus::PUBLISHED->value ? now() : null,
            ]);

            $article->categories()->sync($this->category_ids);

            $this->uploadMedia($this->thumbnail, $article, 'thumbnail');
        });

        return $article->title;
    }

    public function update(): void
    {
        $this->validate();

        DB::transaction(function () {
            $this->article->update(array_merge(
                [
                    'title' => $this->title,
                    'excerpt' => $this->excerpt,
                    'content' => $this->content,
                    'status' => $this->status,
                ],
                ($this->status == ArticleStatus::PUBLISHED->value && ! $this->article->published_at)
                    ? ['published_at' => now()]
                    : []
            ));

            $this->article->categories()->sync($this->category_ids);

            $this->syncMedia($this->thumbnail, $this->article, 'thumbnail');
            $this->uploadMedia($this->thumbnail, $this->article, 'thumbnail');
        });
    }
}
