<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;

class Form extends BaseModel
{
    protected $casts = [
        'registers' => 'array'
    ];

    public function Category(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'category_uuid', 'uuid');
    }



    public function getRegistersAttribute($value): array
    {
        if (is_array($value)) {
            return $value;
        }

        $decoded = json_decode($value, true);
        return is_array($decoded) ? $decoded : [];
    }

    public function registerQuery()
    {
        $uuids = $this->registers;
        if (empty($uuids)) {
            return Register::query()->whereRaw('1 = 0');
        }

        $list = implode(',', $uuids);
        return Register::whereRaw('FIND_IN_SET(uuid, ?) > 0', [$list]);
    }

}