<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/**
 * Base Repository Class
 *
 * @package Wisewand
 * @subpackage Wisewand/includes/Repositories
 *
 * phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- False positive from Plugin Checker
 * All queries in this file use $wpdb->prepare() with proper placeholders.
 * The checker incorrectly flags the prepare() method calls themselves as "not prepared".
 * phpcs:disable WordPress.DB.DirectDatabaseQuery -- Repository pattern requires direct queries for custom tables
 * phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom tables don't use WordPress object cache
 */

namespace Wisewand\Repositories;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Base_Repository
 *
 * Abstract base class for all repositories.
 */
abstract class Base_Repository {

	/**
	 * WordPress Database Access Abstraction Object.
	 *
	 * @var \wpdb
	 */
	protected $wpdb;

	/**
	 * The table name (without prefix).
	 *
	 * @var string
	 */
	protected $table_slug;

	/**
	 * The full table name (with prefix).
	 *
	 * @var string
	 */
	protected $table_name;

	/**
	 * Initialize the repository.
	 *
	 * @param string $table_slug The table name without prefix (e.g., 'wisewand_editorial_calendar').
	 */
	public function __construct( $table_slug = '' ) {
		global $wpdb;
		$this->wpdb = $wpdb;

		if ( ! empty( $table_slug ) ) {
			$this->table_slug = $table_slug;
			$this->table_name = $this->wpdb->prefix . $table_slug;
		}
	}

	/**
	 * Get the full table name.
	 *
	 * @return string
	 */
	public function get_table_name() {
		return $this->table_name;
	}

	/**
	 * Check if the table exists.
	 *
	 * @return bool
	 */
	public function table_exists() {
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from $wpdb->prefix is safe.
		return $this->wpdb->get_var( $this->wpdb->prepare( 'SHOW TABLES LIKE %s', $this->table_name ) ) === $this->table_name;
	}

	/**
	 * Get the last error from the database.
	 *
	 * @return string
	 */
	public function get_last_error() {
		return $this->wpdb->last_error;
	}

	/**
	 * Start a database transaction.
	 */
	public function start_transaction() {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$this->wpdb->query( 'START TRANSACTION' );
	}

	/**
	 * Commit a database transaction.
	 */
	public function commit() {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$this->wpdb->query( 'COMMIT' );
	}

	/**
	 * Rollback a database transaction.
	 */
	public function rollback() {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$this->wpdb->query( 'ROLLBACK' );
	}
}
