<?php

namespace App\Models;

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

class Document extends Model
{
    use HasFactory;

    protected $fillable = [
        'business_trip_id',
        'business_trip_attendance_id',
        'user_id',
        'type',
        'file_name',
        'file_path',
        'file_type',
        'file_size',
        'description',
        'status',
        'approved_by',
        'approved_at',
        'rejection_reason',
        'hardcopy_received',
        'hardcopy_received_by',
        'hardcopy_received_at',
    ];

    protected function casts(): array
    {
        return [
            'approved_at' => 'datetime',
            'hardcopy_received' => 'boolean',
            'hardcopy_received_at' => 'datetime',
            'file_size' => 'integer',
        ];
    }

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

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

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

    public function approver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    public function hardcopyReceiver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'hardcopy_received_by');
    }

    // Scopes
    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopeRejected($query)
    {
        return $query->where('status', 'rejected');
    }

    public function scopeHardcopyPending($query)
    {
        return $query->where('status', 'approved')
            ->where('hardcopy_received', false);
    }

    // Helper methods
    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    public function isRejected(): bool
    {
        return $this->status === 'rejected';
    }

    public function getTypeLabelAttribute(): string
    {
        return match ($this->type) {
            'tiket_pesawat' => 'Tiket Pesawat',
            'tiket_kereta' => 'Tiket Kereta',
            'tiket_bus' => 'Tiket Bus',
            'boarding_pass' => 'Boarding Pass',
            'hotel_invoice' => 'Invoice Hotel',
            'kwitansi_bbm' => 'Kwitansi BBM',
            'toll_receipt' => 'Bukti Tol',
            'surat_tugas' => 'Surat Tugas',
            'lainnya' => 'Lainnya',
            default => $this->type,
        };
    }

    public function getFileSizeFormattedAttribute(): string
    {
        $bytes = $this->file_size;

        if ($bytes >= 1073741824) {
            return number_format($bytes / 1073741824, 2) . ' GB';
        } elseif ($bytes >= 1048576) {
            return number_format($bytes / 1048576, 2) . ' MB';
        } elseif ($bytes >= 1024) {
            return number_format($bytes / 1024, 2) . ' KB';
        } else {
            return $bytes . ' bytes';
        }
    }
}
