<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class JobApplication extends Model
{
    use HasFactory, LogsActivity;

    protected $fillable = [
        'job_posting_id',
        'department_id',
        'full_name',
        'email',
        'phone',
        'cover_letter',
        'cv_path',
        'cv_original_name',
        'cv_scan_status',
        'cv_scan_result',
        'cv_scanned_at',
        'linkedin_url',
        'status',
        'internal_notes',
        'source',
        'kvkk_consent',
        'kvkk_consent_at',
        'kvkk_consent_ip',
        'reviewed_by',
        'purged_at',
    ];

    protected $casts = [
        'kvkk_consent'    => 'boolean',
        'kvkk_consent_at' => 'datetime',
        'cv_scan_result'  => 'array',
        'cv_scanned_at'   => 'datetime',
        'purged_at'       => 'datetime',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['status', 'reviewed_by', 'cv_scan_status', 'purged_at', 'internal_notes'])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }

    public function jobPosting()
    {
        return $this->belongsTo(JobPosting::class);
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    public function reviewer()
    {
        return $this->belongsTo(User::class, 'reviewed_by');
    }

    public function interviews()
    {
        return $this->hasMany(Interview::class, 'job_application_id');
    }

    public function getCvScanStatusLabelAttribute(): string
    {
        return match ($this->cv_scan_status ?? 'pending') {
            'pending'  => 'Taranıyor...',
            'clean'    => 'Temiz',
            'infected' => 'Virüs Tespit Edildi',
            'failed'   => 'Tarama Başarısız',
            'skipped'  => 'Taranmadı',
            default    => 'Bilinmiyor',
        };
    }

    public function getCvScanStatusColorAttribute(): string
    {
        return match ($this->cv_scan_status ?? 'pending') {
            'pending'  => 'yellow',
            'clean'    => 'green',
            'infected' => 'red',
            'failed'   => 'orange',
            'skipped'  => 'gray',
            default    => 'gray',
        };
    }

    public function getStatusLabelAttribute(): string
    {
        return match ($this->status) {
            'yeni' => 'Yeni',
            'incelendi' => 'İncelendi',
            'görüşmeye_çağrıldı' => 'Görüşmeye Çağrıldı',
            'reddedildi' => 'Reddedildi',
            'işe_alındı' => 'İşe Alındı',
            default => $this->status,
        };
    }

    public function getStatusColorAttribute(): string
    {
        return match ($this->status) {
            'yeni' => 'blue',
            'incelendi' => 'yellow',
            'görüşmeye_çağrıldı' => 'purple',
            'reddedildi' => 'red',
            'işe_alındı' => 'green',
            default => 'gray',
        };
    }
}