<?php

namespace App\Models;

use App\Core\Model;

class DynamicFormField extends Model
{
    protected $table = 'dynamic_form_fields';
    protected $primaryKey = 'id';

    public function getByForm($formId)
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE form_id = ? ORDER BY sort_order ASC");
        $stmt->execute([$formId]);
        return $stmt->fetchAll(\PDO::FETCH_OBJ);
    }

    /**
     * Parse options string into array.
     * Accepts comma or newline separated values and trims each option.
     */
    public function parseOptions($options)
    {
        if (empty($options)) return [];
        $parts = preg_split('/\r\n|\r|\n|,/', $options);
        $out = [];
        foreach ($parts as $p) {
            $t = trim($p);
            if ($t !== '') $out[] = $t;
        }
        return $out;
    }
}
