<?php
/**
 * AP Family Trees - Children Table
 *
 * @package    APFT_Family_Trees
 * @subpackage APFT_Family_Trees/includes/db
 * @since      2.0.0
 */

namespace AncestorPress\Core;

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

class Children_Table {

	public static function table() {
		return Tables::children();
	}

	public static function get_by_tree( $gedcom ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE gedcom = %s ORDER BY ordernum ASC', self::table(), $gedcom ) );
	}

	public static function get_by_family( $gedcom, $familyID ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE gedcom = %s AND familyID = %s ORDER BY ordernum ASC', self::table(), $gedcom, $familyID ) );
	}

	public static function get_by_person( $gedcom, $personID ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE gedcom = %s AND personID = %s', self::table(), $gedcom, $personID ) );
	}

	public static function insert( $data ) {
		global $wpdb;
		$now  = Tables::now();
		$data = array_merge(
			array( 'created_by_user_id' => get_current_user_id(), 'created_at' => $now, 'updated_at' => $now ),
			$data
		);
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$result = $wpdb->insert( self::table(), $data );
		return $result ? (int) $wpdb->insert_id : false;
	}

	public static function delete_by_gedcom( $gedcom ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->delete( self::table(), array( 'gedcom' => $gedcom ) );
	}

	/**
	 * Delete a specific child-to-family link by person and family.
	 *
	 * @param  string $gedcom   Tree slug.
	 * @param  string $familyID Family GEDCOM ID.
	 * @param  string $personID Person GEDCOM ID.
	 * @return int|false
	 */
	/**
	 * Delete all child links for a person (any family).
	 *
	 * @param  string $gedcom   Tree slug.
	 * @param  string $personID Person GEDCOM ID.
	 * @return int|false
	 */
	public static function delete_by_person( $gedcom, $personID ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->delete( self::table(), array( 'gedcom' => $gedcom, 'personID' => $personID ) );
	}

	/**
	 * Delete all child links for a family.
	 *
	 * @param  string $gedcom   Tree slug.
	 * @param  string $familyID Family GEDCOM ID.
	 * @return int|false
	 */
	public static function delete_by_family( $gedcom, $familyID ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->delete( self::table(), array( 'gedcom' => $gedcom, 'familyID' => $familyID ) );
	}

	public static function delete_child_link( $gedcom, $familyID, $personID ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->delete(
			self::table(),
			array(
				'gedcom'   => $gedcom,
				'familyID' => $familyID,
				'personID' => $personID,
			)
		);
	}
}
