<?php

declare(strict_types=1);

namespace App\Models;


class FileList extends Model
{

    protected ?string $table = 'file_list';

    protected array $fillable = [
        'uid',
        'useruid_access_list',
        'hashkey',
        'contentuid',
        'title',
        'filename',
        'cdn_url',
        'is_public',
        'file_type',
        'description',
        'tags',
        'hidden',
        'categories',
        'created_by',
        'updated_by',
        'details'
    ];

    protected array $casts = [
        'hidden' => 'integer',
        'is_public' => 'boolean',
        'contentuid' => 'integer',
        'created_by' => 'integer',
        'updated_by' => 'integer',
        'details' => 'array',
        'useruid_access_list' => 'array',
        'tags'=>'array',
    ];


    /**
     * Reference to file content
     */
    public function fileContent()
    {
        return $this->belongsTo(FileContent::class, 'contentuid');
    }

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

    public function updater()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * Resolve a usable URL for this file. Prefers `cdn_url` (e.g. a
     * jsDelivr-fronted CDN asset) when set; otherwise falls back to the
     * local /RequestData/File/{hash} route.
     */
    public function resolvedUrl(): string
    {
        $cdn = trim((string) ($this->cdn_url ?? ''));
        if ($cdn !== '') {
            return $cdn;
        }
        return "/RequestData/File/{$this->hashkey}";
    }
}
