<?php

namespace App\Traits;

use App\Models\Product\Product;
use Illuminate\Support\Str;

trait HasProduct
{
    /**
     * Boot the trait
     */
    protected static function bootHasProduct()
    {
        // Ürün oluşturulduğunda Product kaydı da oluştur
        static::created(function ($model) {
            if (!$model->product_id) {
                $product = Product::create([
                    'agency_id' => $model->agency_id,
                    'product_type_id' => $model->getProductTypeId(),
                    'productable_type' => get_class($model),
                    'productable_id' => $model->id,
                    'code' => $model->code,
                    'name' => $model->name,
                    'slug' => $model->slug,
                    'description' => $model->description ?? $model->short_description,
                    'base_price' => $model->getBasePrice(),
                    'currency' => $model->currency ?? 'TRY',
                    'is_active' => $model->is_active ?? true,
                    'is_featured' => $model->is_featured ?? false,
                    'is_bookable' => $model->is_bookable ?? true,
                    'published_at' => $model->published_at ?? null,
                    'created_by' => $model->created_by ?? null,
                    'updated_by' => $model->updated_by ?? null,
                ]);

                $model->update(['product_id' => $product->id]);
            }
        });

        // Ürün güncellendiğinde Product kaydı da güncelle
        static::updated(function ($model) {
            if ($model->product_id && $model->product) {
                $model->product->update([
                    'name' => $model->name,
                    'slug' => $model->slug,
                    'description' => $model->description ?? $model->short_description,
                    'base_price' => $model->getBasePrice(),
                    'currency' => $model->currency ?? 'TRY',
                    'is_active' => $model->is_active ?? true,
                    'is_featured' => $model->is_featured ?? false,
                    'is_bookable' => $model->is_bookable ?? true,
                    'published_at' => $model->published_at ?? null,
                    'updated_by' => $model->updated_by ?? null,
                ]);
            }
        });

        // Ürün silindiğinde Product kaydı da silinir (cascade ile otomatik)
        static::deleting(function ($model) {
            if ($model->product_id && $model->product) {
                $model->product->delete();
            }
        });
    }

    /**
     * Her ürün türü kendi product_type_id'sini döner
     * Bu metod child class'ta implement edilmelidir
     */
    abstract protected function getProductTypeId(): int;

    /**
     * Her ürün türü kendi base fiyatını döner
     * Bu metod child class'ta implement edilmelidir
     */
    abstract protected function getBasePrice(): ?float;

    /**
     * Product ilişkisi
     */
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }

    /**
     * Kategorilere erişim (product üzerinden)
     */
    public function categories()
    {
        return $this->product ? $this->product->categories() : collect();
    }

    /**
     * Kategori atama helper metodu
     */
    public function syncCategories(array $categoryIds)
    {
        if ($this->product) {
            return $this->product->categories()->sync($categoryIds);
        }
        return null;
    }

    /**
     * View count artırma
     */
    public function incrementProductViewCount()
    {
        if ($this->product) {
            $this->product->incrementViewCount();
        }
    }

    /**
     * Booking count artırma
     */
    public function incrementProductBookingCount()
    {
        if ($this->product) {
            $this->product->incrementBookingCount();
        }
    }
}
