<?php

namespace App\Models;

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

class Bug extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'project_id', 'module_id', 'task_id', 'reported_by', 'assigned_to', 
        'title', 'description', 'severity', 'status', 'reopen_count',
        'target_fix_date', 'browser', 'device', 'os', 'screenshot_path'
    ];

    protected function casts(): array
    {
        return ['target_fix_date' => 'date'];
    }

    public function project(): BelongsTo { return $this->belongsTo(Project::class); }
    public function module(): BelongsTo { return $this->belongsTo(ProjectModule::class, 'module_id'); }
    public function task(): BelongsTo { return $this->belongsTo(Task::class); }
    public function reporter(): BelongsTo { return $this->belongsTo(User::class, 'reported_by'); }
    public function assignee(): BelongsTo { return $this->belongsTo(User::class, 'assigned_to'); }
    public function histories(): HasMany { return $this->hasMany(BugHistory::class); }
    public function comments(): HasMany { return $this->hasMany(TaskComment::class, 'bug_id'); }
}
