<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tribunal extends Model
{
    protected $fillable = [
        'anime_id', 'episode_id', 'created_by', 'question',
        'side_a', 'side_b', 'extra_sides', 'status', 'verdict', 'closes_at',
    ];

    protected $casts = [
        'closes_at'   => 'datetime',
        'extra_sides' => 'array',
    ];

    // Tüm tarafları ['a'=>'Haklıydı', 'b'=>'Haksızdı', 'c'=>'...'] formatında döndür
    public function allSides(): array
    {
        $sides = ['a' => $this->side_a, 'b' => $this->side_b];
        foreach (($this->extra_sides ?? []) as $i => $label) {
            $sides[chr(99 + $i)] = $label; // c, d, e, ...
        }
        return $sides;
    }

    public function anime()      { return $this->belongsTo(Anime::class); }
    public function episode()    { return $this->belongsTo(Episode::class); }
    public function creator()    { return $this->belongsTo(User::class, 'created_by'); }
    public function votes()      { return $this->hasMany(TribunalVote::class); }
    public function arguments()  { return $this->hasMany(TribunalArgument::class); }

    public function voteCountA() { return $this->votes()->where('side', 'a')->count(); }
    public function voteCountB() { return $this->votes()->where('side', 'b')->count(); }
}
