<?php

namespace App\Models\Ecommerce\Product\Brand;

use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Spatie\Translatable\HasTranslations;

class Brand extends Model implements HasMedia, Sortable
{
    use HasFactory;
    use HasSlug;
    use HasTranslations;
    use InteractsWithMedia;
    use SortableTrait;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'slug',
        'type',
        'website',
        'description',
        'seo_title',
        'seo_description',
        'is_visible',
        'sort_order',
    ];

    /**
     * Translatable fields
     *
     * @var array<string>
     */
    public $translatable = ['description'];

    /**
     * Sortable attributes
     *
     * @var array<string, mixed>
     */
    public $sortable = [
        'order_column_name' => 'sort_order',
        'sort_when_creating' => true,
    ];

    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    /**
     * Register media collections
     */
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('uploads')
            ->singleFile()
            ->acceptsMimeTypes(['image/jpg', 'image/jpeg', 'image/png'])
            ->useFallbackUrl(
                sprintf('%s/logo-space.png', config('app.url'))
            );
    }

    /**
     * Register Media Conversions
     */
    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb200x200')
            ->fit(Manipulations::FIT_CONTAIN, 200, 200);

        $this->addMediaConversion('thumb400x400')
            ->fit(Manipulations::FIT_CONTAIN, 400, 400);

        $this->addMediaConversion('thumb720x720')
            ->fit(Manipulations::FIT_CONTAIN, 720, 720);

        $this->addMediaConversion('thumb800x800')
            ->fit(Manipulations::FIT_CONTAIN, 800, 800);

        $this->addMediaConversion('thumb1200x1200')
            ->fit(Manipulations::FIT_CONTAIN, 1200, 1200);

        $this->addMediaConversion('thumb288x431')
            ->fit(Manipulations::FIT_CONTAIN, 288, 431);

        $this->addMediaConversion('thumb270x350')
            ->fit(Manipulations::FIT_CONTAIN, 270, 350);
    }

    /**
     * Brand thumbnail
     */
    public function thumbnail(string $size = '200x200'): string
    {
        return $this->getFirstMediaUrl('uploads', 'thumb'.$size);
    }

    /**
     * Original image
     */
    public function image(): string
    {
        return $this->getFirstMediaUrl('uploads');
    }

    /**
     * Brand Products
     */
    public function products(): HasMany
    {
        return $this->hasMany(Product::class);
    }

    /**
     * Scope for visable models
     */
    public function scopeEnabled(Builder $query): void
    {
        $query->where('is_visible', true);
    }

    /**
     * Brand Types
     */
    public static function types(): array
    {
        return [
            'market',
        ];
    }

    /**
     * Check if brand belongs to market
     *
     * @return bool
     */
    public function isMarketCategory(): string
    {
        return $this->type === 'market';
    }

    /**
     * Brand's Products web page
     */
    public function productsPage(): string
    {
        return route('web.brands.products', ['brand' => $this->slug]);
    }
}
