<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class Competition extends BaseModel
{
    protected $table = 'competitions';

    // ✅ CI: useSoftDeletes = true
    use SoftDeletes;

    // ✅ CI: useTimestamps = true (created_at/updated_at) + deleted_at for soft deletes
    public $timestamps = true;

    protected $fillable = [
        'title',
        'semester',
        'school_year',
        'class_section_id',
        'start_date',
        'end_date',
        'winners_count',
        'prize_per_point',
        'is_published',
        'published_at',
        'created_by',
        'created_at',
        'updated_at',
        'deleted_at',
        'is_locked',
        'locked_at',
        'locked_by',
    ];

    protected $casts = [
        'class_section_id' => 'integer',
        'winners_count'    => 'integer',
        'prize_per_point'  => 'decimal:2', // adjust precision if needed

        'is_published'     => 'boolean',
        'is_locked'        => 'boolean',

        // change to 'date' if your columns are DATE (not DATETIME)
        'start_date'       => 'date',
        'end_date'         => 'date',
        'published_at'     => 'datetime',
        'locked_at'        => 'datetime',

        'locked_by'        => 'integer',
        'created_by'       => 'integer',
    ];

    /* Optional relationships */
    public function classSection()
    {
        return $this->belongsTo(ClassSection::class, 'class_section_id');
    }

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

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

    public function classWinners()
    {
        return $this->hasMany(CompetitionClassWinner::class, 'competition_id');
    }
}