<?php

namespace App\Models;

use Database\Factories\RouteFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;

/**
 * @property int $feed_id
 * @property int $platform_channel_id
 * @property bool $is_active
 * @property int $priority
 * @property bool|null $auto_approve
 * @property Carbon $created_at
 * @property Carbon $updated_at
 */
class Route extends Model
{
    /** @use HasFactory<RouteFactory> */
    use HasFactory;

    protected $table = 'routes';

    // Laravel doesn't handle composite primary keys well, so we'll use regular queries
    protected $primaryKey = null;

    public $incrementing = false;

    protected $fillable = [
        'feed_id',
        'platform_channel_id',
        'is_active',
        'priority',
        'auto_approve',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'auto_approve' => 'boolean',
    ];

    /**
     * @return BelongsTo<Feed, $this>
     */
    public function feed(): BelongsTo
    {
        return $this->belongsTo(Feed::class);
    }

    /**
     * @return BelongsTo<PlatformChannel, $this>
     */
    public function platformChannel(): BelongsTo
    {
        return $this->belongsTo(PlatformChannel::class);
    }

    /**
     * @return HasMany<Keyword, $this>
     */
    public function keywords(): HasMany
    {
        return $this->hasMany(Keyword::class, 'feed_id', 'feed_id')
            ->where('platform_channel_id', $this->platform_channel_id);
    }

    /**
     * @return HasMany<RouteArticle, $this>
     */
    public function routeArticles(): HasMany
    {
        return $this->hasMany(RouteArticle::class, 'feed_id', 'feed_id')
            ->where('platform_channel_id', $this->platform_channel_id);
    }
}
