<?php

namespace App\Domain\Website\Models;

use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;

class WebsiteNews extends Model
{
    use BelongsToAcademy, HasUuid, SoftDeletes;

    protected $table = 'website_news';

    protected $fillable = [
        'academy_id',
        'channel',
        'title',
        'title_en',
        'slug',
        'body',
        'body_en',
        'excerpt',
        'excerpt_en',
        'category',
        'category_en',
        'published_at',
        'is_featured',
        'sort_order',
        'created_by',
    ];

    protected $casts = [
        'published_at' => 'datetime',
        'is_featured' => 'boolean',
        'sort_order' => 'integer',
    ];

    /**
     * Content published to a given surface.
     *
     * `both` is the default and the common case: the portal and the public
     * site read the same tables, and this is the one column that decides where
     * an item appears — instead of a second CMS with a second migration
     * surface to keep in step.
     */
    public function scopeForChannel(\Illuminate\Database\Eloquent\Builder $query, string $channel): \Illuminate\Database\Eloquent\Builder
    {
        return $query->whereIn('channel', [$channel, 'both']);
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function image(): MorphOne
    {
        return $this->morphOne(Media::class, 'mediable')
            ->where('collection', 'news_image');
    }
}
