<?php

namespace App\Livewire\Forms\Studio\Loyalty;

use App\Enums\IsShow;
use App\Enums\RewardCategory;
use App\Models\Reward;
use App\Rules\UnsignedInteger;
use App\Traits\Media\WithMediaHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;

class RewardForm extends Form
{
    use WithMediaHandler;

    public ?Reward $reward = null;

    public string $name = '';

    public string $points = '';

    public ?string $stock = null;

    public int $category = RewardCategory::REGULAR->value;

    public int $is_show = IsShow::SHOW->value;

    public string $start_date = '';

    public ?string $end_date = null;

    public array $tier_ids = [];

    public array $thumbnail = [];

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:30'],
            'points' => ['required', new UnsignedInteger],
            'stock' => ['nullable', new UnsignedInteger],
            'category' => ['required', Rule::in(RewardCategory::cases())],
            'is_show' => ['required', Rule::in(IsShow::cases())],
            'start_date' => ['required', 'date'],
            'end_date' => ['nullable', 'date', 'after_or_equal:start_date'],
            'tier_ids' => ['required', 'array', 'min:1'],
            'tier_ids.*' => Rule::exists('tiers', 'id'),
            'thumbnail' => 'array',
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'name' => 'nama',
            'points' => 'poin',
            'stock' => 'stok',
            'category' => 'kategori',
            'is_show' => 'visibilitas',
            'start_date' => 'tanggal mulai',
            'end_date' => 'tanggal berakhir',
            'tier_ids' => 'tier',
            'tier_ids.*' => 'tier',
        ];
    }

    public function setReward(Reward $reward): void
    {
        $this->reward = $reward;

        $this->name = $reward->name;
        $this->points = formatCurrencyNumber($reward->points);
        $this->stock = formatCurrencyNumber($reward->stock);
        $this->category = $reward->category->value;
        $this->is_show = $reward->is_show->value;
        $this->start_date = $reward->start_date;
        $this->end_date = $reward->end_date;
        $this->tier_ids = $reward->tiers->pluck('id')->toArray();
        $this->thumbnail = $this->mapMediaCollection($reward->getMedia('thumbnail'));
    }

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

        DB::transaction(function () {
            $reward = Reward::create([
                'name' => $this->name,
                'points' => parseRupiahToInt($this->points),
                'stock' => parseRupiahToInt($this->stock),
                'category' => $this->category,
                'is_show' => $this->is_show,
                'start_date' => $this->start_date,
                'end_date' => $this->end_date,
            ]);

            $reward->tiers()->sync($this->tier_ids);

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

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

        DB::transaction(function () {
            $this->reward->update([
                'name' => $this->name,
                'points' => parseRupiahToInt($this->points),
                'stock' => $this->stock ? parseRupiahToInt($this->stock) : null,
                'category' => $this->category,
                'is_show' => $this->is_show,
                'start_date' => $this->start_date,
                'end_date' => $this->end_date,
            ]);

            $this->reward->tiers()->sync($this->tier_ids);

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