<?php

namespace App\Models;

use App\HasThumbnail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Image extends Model implements HasThumbnail
{
    use HasFactory;

    /**
     * The model's default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'isCover' => false,
        'isProcessing' => true,
        'lightboxWidth' => 0,
        'lightboxHeight' => 0,
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['album_id', 'lightboxWidth', 'lightboxHeight', 'isCover', 'isProcessing'];

    public function album(): BelongsTo
    {
        return $this->belongsTo(Album::class);
    }

    public function getThumbnail() : string {
        return route('image.thumbnail', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
    }

    public function getDownload() : string {
        return route('image.download', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
    }

    public function setLightboxSize(int $width, int $height) : void {
        $this->lightboxWidth = $width;
        $this->lightboxHeight = $height;
        $this->save();
    }

    public function getLightboxAttribute() : array {
        return [
            'location' => route('image.lightbox', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp,
            'width' => $this->lightboxWidth,
            'height' => $this->lightboxHeight,
        ];
    }
}
