<?php
// app/Models/FileAttachment.php
namespace App\Models;

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

class FileAttachment extends Model
{
    use HasFactory;

    protected $table = 'file_attachments';

    protected $fillable = [
        'file_id',
        'notice_update_id',
        'attachment_type_id', // Add this
        'file_name',
        'doc_name',
        'doc_type',
        'doc_size',
        'created_by',
        'updated_by',
        'active'
    ];

    protected $casts = [
        'active' => 'boolean',
        'doc_size' => 'integer',
    ];

    /**
     * Get the file that owns this attachment
     */
    public function file(): BelongsTo
    {
        return $this->belongsTo(File::class, 'file_id');
    }

    /**
     * Get the notice update that owns this attachment
     */
    public function noticeUpdate(): BelongsTo
    {
        return $this->belongsTo(NoticeUpdate::class, 'notice_update_id');
    }

    /**
     * Get the attachment type of this attachment
     */
    public function attachmentType(): BelongsTo
    {
        return $this->belongsTo(AttachmentType::class, 'attachment_type_id');
    }

    /**
     * Get the user who created this attachment
     */
    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get the user who last updated this attachment
     */
    public function updatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * Get the file size in human readable format
     */
    public function getHumanReadableSizeAttribute(): string
    {
        $bytes = $this->doc_size;
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];

        for ($i = 0; $bytes > 1024; $i++) {
            $bytes /= 1024;
        }

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

    /**
     * Get the file extension
     */
    public function getFileExtensionAttribute(): string
    {
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
    }

    /**
     * Get the file icon based on extension
     */
    public function getFileIconAttribute(): string
    {
        $extension = strtolower($this->file_extension);
        
        $icons = [
            'pdf' => 'bi-file-pdf',
            'doc' => 'bi-file-word',
            'docx' => 'bi-file-word',
            'xls' => 'bi-file-excel',
            'xlsx' => 'bi-file-excel',
            'ppt' => 'bi-file-ppt',
            'pptx' => 'bi-file-ppt',
            'jpg' => 'bi-file-image',
            'jpeg' => 'bi-file-image',
            'png' => 'bi-file-image',
            'gif' => 'bi-file-image',
            'txt' => 'bi-file-text',
            'zip' => 'bi-file-zip',
            'rar' => 'bi-file-zip',
        ];

        return $icons[$extension] ?? 'bi-file-earmark';
    }

    /**
     * Get the file color based on extension
     */
    public function getFileColorAttribute(): string
    {
        $extension = strtolower($this->file_extension);
        
        $colors = [
            'pdf' => 'danger',
            'doc' => 'primary',
            'docx' => 'primary',
            'xls' => 'success',
            'xlsx' => 'success',
            'ppt' => 'warning',
            'pptx' => 'warning',
            'jpg' => 'info',
            'jpeg' => 'info',
            'png' => 'info',
            'gif' => 'info',
            'txt' => 'secondary',
            'zip' => 'secondary',
            'rar' => 'secondary',
        ];

        return $colors[$extension] ?? 'secondary';
    }
}