<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
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\Database\Eloquent\SoftDeletes;

/**
 * @property string $id
 * @property string $tenant_id
 * @property string $slug
 * @property string $title
 * @property string|null $description
 * @property string $category
 * @property array|null $tags
 * @property string|null $author_id
 * @property string|null $file_path
 * @property int $file_size
 * @property string|null $mime
 * @property int $download_count
 * @property int $view_count
 * @property float $rating_avg
 * @property int $rating_count
 * @property bool $is_free
 * @property int|null $price
 * @property int|null $required_member_level
 * @property string $status
 */
class Resource extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'tenant_id', 'slug', 'title', 'description', 'category', 'tags',
        'author_id', 'file_path', 'file_size', 'mime',
        'download_count', 'view_count', 'rating_avg', 'rating_count',
        'is_free', 'price', 'required_member_level',
        'status', 'published_at',
    ];

    protected function casts(): array
    {
        return [
            'tags' => 'array',
            'file_size' => 'integer',
            'download_count' => 'integer',
            'view_count' => 'integer',
            'rating_avg' => 'float',
            'rating_count' => 'integer',
            'is_free' => 'boolean',
            'price' => 'integer',
            'required_member_level' => 'integer',
            'published_at' => 'datetime',
        ];
    }

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

    public function downloads(): HasMany
    {
        return $this->hasMany(ResourceDownload::class);
    }

    public function ratings(): HasMany
    {
        return $this->hasMany(ResourceRating::class);
    }
}
