<?php

namespace App\Livewire\Activities;

use App\Domain\Training\Models\Activity;
use App\Domain\Training\Services\ActivityService;
use App\Domain\Shared\Exceptions\DomainException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('إدارة النشاط')]
class ActivityForm extends Component
{
    public ?Activity $activity = null;
    public bool $editing = false;

    public string $name = '';
    public string $name_ar = '';
    public string $slug = '';
    public string $description = '';
    public string $description_ar = '';
    public string $icon = '';
    public string $color = '#3B82F6';
    public string $category = 'sport';
    public bool $is_active = true;
    public int $sort_order = 0;

    public function mount(?Activity $activity = null): void
    {
        if ($activity && $activity->exists) {
            $this->activity = $activity;
            $this->editing = true;
            $this->name = $activity->name ?? '';
            $this->name_ar = $activity->name_ar;
            $this->slug = $activity->slug ?? '';
            $this->description = $activity->description ?? '';
            $this->description_ar = $activity->description_ar ?? '';
            $this->icon = $activity->icon ?? '';
            $this->color = $activity->color ?? '#3B82F6';
            $this->category = $activity->category->value ?? $activity->category;
            $this->is_active = $activity->is_active;
            $this->sort_order = $activity->sort_order;
        }
    }

    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'name_ar' => 'required|string|max:255',
            'slug' => 'nullable|string|max:100',
            'description' => 'nullable|string',
            'description_ar' => 'nullable|string',
            'icon' => 'nullable|string|max:100',
            'color' => 'nullable|string|max:7',
            'category' => 'required|in:sport,art,academic,fitness,other',
            'is_active' => 'boolean',
            'sort_order' => 'integer|min:0',
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => 'اسم النشاط بالإنجليزية مطلوب',
            'name.max' => 'اسم النشاط بالإنجليزية يجب ألا يتجاوز 255 حرف',
            'name_ar.required' => 'اسم النشاط بالعربية مطلوب',
            'name_ar.max' => 'اسم النشاط بالعربية يجب ألا يتجاوز 255 حرف',
            'category.required' => 'تصنيف النشاط مطلوب',
            'category.in' => 'تصنيف النشاط غير صالح',
            'sort_order.integer' => 'الترتيب يجب أن يكون رقم صحيح',
            'sort_order.min' => 'الترتيب يجب أن يكون 0 أو أكثر',
        ];
    }

    public function save(ActivityService $service): void
    {
        $this->validate();

        try {
            $data = [
                'name' => $this->name,
                'name_ar' => $this->name_ar,
                'slug' => $this->slug ?: null,
                'description' => $this->description ?: null,
                'description_ar' => $this->description_ar ?: null,
                'icon' => $this->icon ?: null,
                'color' => $this->color ?: null,
                'category' => $this->category,
                'is_active' => $this->is_active,
                'sort_order' => $this->sort_order,
            ];

            if ($this->editing) {
                $service->update($this->activity, $data);
                session()->flash('success', __('تم تحديث النشاط بنجاح'));
            } else {
                $service->create($data, auth()->user());
                session()->flash('success', __('تم إنشاء النشاط بنجاح'));
            }

            $this->redirect(route('activities.list'), navigate: true);
        } catch (DomainException $e) {
            session()->flash('error', $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.activities.activity-form');
    }
}
