<?php

namespace Digtective\Repository\FormSubmissionRepository;

use Digtective\Repository\BaseRepository;

class FormSubmissionRepository extends BaseRepository implements FormSubmissionRepositoryInterface{
    private string $tableName;

    public function __construct(){
        parent::__construct();
        $this->tableName = $this->DB->prefix . "digtective_woocommerce_form_submissions";
    }

    public function create(string $form_submission_id, int $order_id){
        $queryResponseString = $this->DB->query(
            $this->DB->prepare(
                "INSERT INTO {$this->tableName} (form_submission_id, order_id) VALUES (%s, %d)",
                $form_submission_id,
                $order_id
            )
        );

        return $queryResponseString;
    }

    public function getByWoocommerceOrderId(int $orderId): string{
        $queryResponse = $this->DB->get_results(
            "SELECT * FROM {$this->tableName} WHERE order_id = $orderId"
        );

        return $queryResponse[0]->form_submission_id ?? "";
    }

    public function doesOrderExist(int $orderId): ?bool{
        $queryResponse = $this->DB->get_results(
            "SELECT COUNT(*) AS `entries` FROM {$this->tableName} WHERE `order_id` = $orderId"
        );

        return $queryResponse[0]->entries > 0 ?? null;
    }
}