<?php

namespace App\Models;
use App\Models\ValidationModel;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Files extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'files';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'size', 'filename', 'related_id', 'related_type'];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['format_file_size'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'name'         => 'required',
        'size'         => 'required',
        'filename'     => 'required',
        'related_id'   => 'required',
        'related_type' => 'required',
    ];

    /**
     * Automatically append created_by.
     *
     * @var void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            $model->created_by = getAuthUserId();
        });
    }

    /**
     * Returns the user who booked in the task.
     *
     * @return \User
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Restrict results to type project only.
     * Optionally you can pass the id of the project
     * to limit results even further.
     *
     * @return Doctrine\Dbal\Query\QueryBuilder
     */
    public function scopeProject($query, $id = null)
    {
        $query->where('related_type', 'project');

        if ($id) {
            $query->where('related_id', $id);
        }

        return $query;
    }

    /**
     * Restrict results to type tasks only.
     * Optionally you can pass the id of the task
     * to limit results even further.
     *
     * @return Doctrine\Dbal\Query\QueryBuilder
     */
    public function scopeTask($query, $id = null)
    {
        $query->where('related_type', 'task');

        if ($id) {
            $query->where('related_id', $id);
        }

        return $query;
    }

    /**
     * Get the user that uploaded this file.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the polymorphic relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function object()
    {
        return $this->morphTo();
    }

    /**
     * Get the formatted file size.
     *
     * @return string
     */
    public function getFormatFileSizeAttribute()
    {
        $bytes = $this->size;
        
        if (!$bytes || $bytes === 0) {
            return '0 B';
        }

        $k = 1024;
        $sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
        $i = floor(log($bytes) / log($k));

        return round($bytes / pow($k, $i), 2) . ' ' . $sizes[$i];
    }
}
