<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Section extends BaseModel
{
    protected $table = 'sections';

    protected $fillable = [
        'section_name',
        'description',
        'updated_at',
        'updated_by',
    ];

    public $timestamps = false;

    protected $casts = [
        'updated_by' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /* ============================================================
     * Relationships (optional)
     * ============================================================
     */

    public function updater(): BelongsTo
    {
        // Change User/Admin model to match your app
        return $this->belongsTo(Admin::class, 'updated_by');
    }

    /* ============================================================
     * Scopes
     * ============================================================
     */

    public function scopeNamed(Builder $q, string $name): Builder
    {
        return $q->where('section_name', $name);
    }

    public function scopeSearch(Builder $q, string $term): Builder
    {
        $term = trim($term);
        if ($term === '') return $q;

        return $q->where(function (Builder $w) use ($term) {
            $w->where('section_name', 'like', "%{$term}%")
              ->orWhere('description', 'like', "%{$term}%");
        });
    }

    /* ============================================================
     * CI-compatible methods
     * ============================================================
     */

    /**
     * Get section by name.
     * CI: getSectionByName()
     */
    public static function getSectionByName(string $sectionName): ?self
    {
        return static::query()->named($sectionName)->first();
    }

    /**
     * Update section details by ID.
     * CI: updateSection() (Laravel automatically updates updated_at)
     */
    public static function updateSection(int $sectionId, array $data): bool
    {
        $section = static::query()->find($sectionId);
        if (!$section) return false;

        // Laravel will set updated_at automatically when saving
        return $section->fill($data)->save();
    }

    /**
     * Delete section by ID.
     * CI: deleteSection()
     */
    public static function deleteSection(int $sectionId): bool
    {
        $section = static::query()->find($sectionId);
        if (!$section) return false;

        return (bool) $section->delete();
    }

    /* ============================================================
     * Optional validation helper (FormRequest/controller)
     * ============================================================
     */

    public static function rules(?int $id = null): array
    {
        return [
            'section_name' => ['required', 'string', 'max:255', 'unique:sections,section_name,' . ($id ?? 'NULL') . ',id'],
            'description'  => ['nullable', 'string', 'max:2000'],
            'updated_by'   => ['nullable', 'integer'],
        ];
    }
}