<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Fillable([
    'user_id',
    'bucket_id',
    'date',
    'storage_bytes',
    'upload_count',
    'upload_bytes',
    'download_count',
    'download_bytes',
    'bandwidth_bytes',
])]
class StorageUsageStat extends Model
{
    /** @use HasFactory<\Database\Factories\StorageUsageStatFactory> */
    use HasFactory;

    /**
     * Get the user for these stats.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the bucket for these stats.
     */
    public function bucket(): BelongsTo
    {
        return $this->belongsTo(FileBucket::class, 'bucket_id');
    }

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'date' => 'date',
            'storage_bytes' => 'integer',
            'upload_count' => 'integer',
            'upload_bytes' => 'integer',
            'download_count' => 'integer',
            'download_bytes' => 'integer',
            'bandwidth_bytes' => 'integer',
        ];
    }

    /**
     * Get human-readable storage size.
     */
    public function getHumanStorageAttribute(): string
    {
        return $this->formatBytes($this->storage_bytes);
    }

    /**
     * Get human-readable bandwidth.
     */
    public function getHumanBandwidthAttribute(): string
    {
        return $this->formatBytes($this->bandwidth_bytes);
    }

    /**
     * Format bytes to human-readable string.
     */
    protected function formatBytes(int $bytes): string
    {
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
        $i = 0;

        while ($bytes >= 1024 && $i < count($units) - 1) {
            $bytes /= 1024;
            $i++;
        }

        return round($bytes, 2).' '.$units[$i];
    }

    /**
     * Record an upload.
     */
    public function recordUpload(int $bytes): void
    {
        $this->upload_count++;
        $this->upload_bytes += $bytes;
        $this->bandwidth_bytes += $bytes;
        $this->save();
    }

    /**
     * Record a download.
     */
    public function recordDownload(int $bytes): void
    {
        $this->download_count++;
        $this->download_bytes += $bytes;
        $this->bandwidth_bytes += $bytes;
        $this->save();
    }

    /**
     * Update storage total.
     */
    public function updateStorage(int $bytes): void
    {
        $this->storage_bytes = $bytes;
        $this->save();
    }

    /**
     * Scope to get stats for a specific date.
     */
    public function scopeForDate($query, $date)
    {
        return $query->where('date', $date);
    }

    /**
     * Scope to get stats for a date range.
     */
    public function scopeDateRange($query, $from, $to)
    {
        return $query->whereBetween('date', [$from, $to]);
    }

    /**
     * Scope to get stats for a specific user.
     */
    public function scopeForUser($query, $userId)
    {
        return $query->where('user_id', $userId);
    }

    /**
     * Scope to get stats for a specific bucket.
     */
    public function scopeForBucket($query, $bucketId)
    {
        return $query->where('bucket_id', $bucketId);
    }
}
