<?php

namespace App\Models;

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

/**
 * PRC-683 — Ham gelen e-postanın (.eml) retention metadata'sı (Katman 2).
 *
 * Dosya storage/app/tickets/{yyyy-mm-dd}/eml/{ticket_id}/{uuid}.eml yolunda
 * ('private' disk). Bu kayıt admin-only chip + policy-korumalı indirme +
 * tickets:prune-eml prune izini için tutulur. Müşteriye gösterilmez (tam
 * header + spoof/dispute verisi içerir).
 *
 * @property int $id
 * @property int $ticket_id
 * @property int|null $reply_id
 * @property string $file_path
 * @property string $uuid
 * @property string|null $from_email
 * @property string|null $subject
 * @property int $file_size
 * @property string|null $scan_status
 * @property string $date_folder
 * @property bool $pruned
 * @property \Illuminate\Support\Carbon|null $pruned_at
 */
class TicketOriginalEmail extends Model
{
    protected $fillable = [
        'ticket_id',
        'reply_id',
        'file_path',
        'uuid',
        'from_email',
        'subject',
        'file_size',
        'scan_status',
        'date_folder',
        'pruned',
        'pruned_at',
    ];

    protected $casts = [
        'file_size' => 'integer',
        'pruned' => 'boolean',
        'pruned_at' => 'datetime',
    ];

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

    public function reply(): BelongsTo
    {
        return $this->belongsTo(TicketReply::class, 'reply_id');
    }

    public function isInfected(): bool
    {
        return $this->scan_status === 'infected';
    }

    public function isDownloadable(): bool
    {
        return ! $this->pruned && ! $this->isInfected();
    }
}
