<?php

namespace App\Models;

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

class Report extends Model
{
    use HasFactory;

    protected $fillable = [
        'reporter_id',
        'reportable_type',
        'reportable_id',
        'reason',
        'description',
        'status',
        'reviewed_by',
        'reviewed_at',
        'admin_notes',
    ];

    protected function casts(): array
    {
        return [
            'reviewed_at' => 'datetime',
        ];
    }

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

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

    public function reportable()
    {
        if ($this->reportable_type === 'item') {
            return $this->belongsTo(Item::class, 'reportable_id');
        } elseif ($this->reportable_type === 'user') {
            return $this->belongsTo(User::class, 'reportable_id');
        }
        return null;
    }

    public function item()
    {
        return $this->belongsTo(Item::class, 'reportable_id');
    }

    public function getReportableAttribute()
    {
        if ($this->reportable_type === 'item') {
            return Item::find($this->reportable_id);
        } elseif ($this->reportable_type === 'user') {
            return User::find($this->reportable_id);
        }
        return null;
    }

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

    public static function getReasonOptions()
    {
        return [
            'spam' => 'Spam or Duplicate Content',
            'inappropriate_content' => 'Inappropriate Content',
            'fake_listing' => 'Fake or Misleading Listing',
            'fraudulent_user' => 'Fraudulent User Activity',
            'harassment' => 'Harassment or Abuse',
            'copyright_violation' => 'Copyright Violation',
            'other' => 'Other'
        ];
    }
}
