<?php

declare(strict_types=1);

namespace App\Modules\Administration\Repositories;

use PDO;
use System\Contracts\DatabaseInterface;
use System\Database\QueryBuilder;

final class DynamicFormFieldOptionRepository
{
    private PDO $pdo;

    public function __construct(
        private QueryBuilder $query,
        DatabaseInterface $database
    ) {
        $this->pdo = $database->getConnection();
    }

    public function byField(
        int $fieldId
    ): array {
        return $this->query
            ->table('dynamic_form_field_options')
            ->where('field_id', $fieldId)
            ->where('status', 1)
            ->orderBy('sort_order', 'ASC')
            ->get();
    }

    public function create(
        array $data
    ): bool {
        return $this->query
            ->table('dynamic_form_field_options')
            ->insert($data);
    }

    public function deleteByField(
        int $fieldId
    ): bool {
        $statement = $this->pdo->prepare(
            '
                DELETE FROM dynamic_form_field_options
                WHERE field_id = ?
            '
        );

        return $statement->execute([
            $fieldId
        ]);
    }
}