<?php

namespace App\Models;

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

class SaranTanggapan extends Model
{
    use HasFactory;

    protected $table = 'SaranTanggapan';
    protected $primaryKey = 'TanggapanID';

    protected $fillable = [
        'KegiatanID',
        'Nama',
        'Peran',
        'NIK',
        'NoTelepon',
        'Email',
        'Gender',
        'KondisiLingkungan',
        'NilaiLokal',
        'Kekhawatiran',
        'Harapan',
        'TingkatKekhawatiran',
        'FotoSelfie',
        'Status',
        'CatatanAdmin',
        'TanggalDiajukan',
        'TanggalDiproses'
    ];

    protected $casts = [
        'TanggalDiajukan' => 'datetime',
        'TanggalDiproses' => 'datetime',
        'TingkatKekhawatiran' => 'integer'
    ];

    /**
     * Get the activity this feedback belongs to
     */
    public function informasiKegiatan()
    {
        return $this->belongsTo(InformasiKegiatan::class, 'KegiatanID');
    }

    /**
     * Scope for pending feedback
     */
    public function scopePending($query)
    {
        return $query->where('Status', 'pending');
    }

    /**
     * Scope for approved feedback
     */
    public function scopeApproved($query)
    {
        return $query->where('Status', 'approved');
    }

    /**
     * Scope for rejected feedback
     */
    public function scopeRejected($query)
    {
        return $query->where('Status', 'rejected');
    }

    /**
     * Scope by role
     */
    public function scopeByRole($query, $role)
    {
        return $query->where('Peran', $role);
    }

    /**
     * Get the rating level description
     */
    public function getRatingDescriptionAttribute()
    {
        $descriptions = [
            1 => 'Sangat Khawatir',
            2 => 'Khawatir',
            3 => 'Netral',
            4 => 'Berharap',
            5 => 'Sangat Berharap'
        ];

        return $descriptions[$this->TingkatKekhawatiran] ?? 'Tidak Diketahui';
    }

    /**
     * Get formatted submission date
     */
    public function getFormattedSubmissionDateAttribute()
    {
        return $this->TanggalDiajukan->format('d M Y H:i');
    }

    /**
     * Get photo URL
     */
    public function getPhotoUrlAttribute()
    {
        if ($this->FotoSelfie) {
            return asset('storage/feedback-photos/' . $this->FotoSelfie);
        }
        return null;
    }

    /**
     * Approve this feedback
     */
    public function approve($adminNote = null)
    {
        $this->update([
            'Status' => 'approved',
            'CatatanAdmin' => $adminNote,
            'TanggalDiproses' => now()
        ]);
    }

    /**
     * Reject this feedback
     */
    public function reject($adminNote = null)
    {
        $this->update([
            'Status' => 'rejected',
            'CatatanAdmin' => $adminNote,
            'TanggalDiproses' => now()
        ]);
    }
}
