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

namespace AncestorPress\Core;

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

class Event_Types_Table {

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

	public static function get_by_tag( $tag ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE tag = %s LIMIT 1', self::table(), $tag ) );
	}

	public static function exists( $tag ) {
		global $wpdb;
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE tag = %s', self::table(), $tag ) );
	}

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

	public static function insert( $data ) {
		global $wpdb;
		$now  = Tables::now();
		$data = array_merge(
			array( 'created_by_user_id' => 0, '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;
	}

	/**
	 * Seed core GEDCOM event types on plugin activation.
	 *
	 * @return void
	 */
	public static function seed() {
		global $wpdb;

		$core_types = array(
			array( 'tag' => 'BIRT', 'display' => 'Birth',         'type' => 'I', 'keep' => 1, 'ordernum' => 1 ),
			array( 'tag' => 'DEAT', 'display' => 'Death',         'type' => 'I', 'keep' => 1, 'ordernum' => 2 ),
			array( 'tag' => 'BURI', 'display' => 'Burial',        'type' => 'I', 'keep' => 1, 'ordernum' => 3 ),
			array( 'tag' => 'CREM', 'display' => 'Cremation',     'type' => 'I', 'keep' => 1, 'ordernum' => 4 ),
			array( 'tag' => 'BAPM', 'display' => 'Baptism',       'type' => 'I', 'keep' => 1, 'ordernum' => 5 ),
			array( 'tag' => 'CHR',  'display' => 'Christening',   'type' => 'I', 'keep' => 1, 'ordernum' => 6 ),
			array( 'tag' => 'CONF', 'display' => 'Confirmation',  'type' => 'I', 'keep' => 0, 'ordernum' => 7 ),
			array( 'tag' => 'CENS', 'display' => 'Census',        'type' => 'I', 'keep' => 1, 'ordernum' => 8 ),
			array( 'tag' => 'EMIG', 'display' => 'Emigration',    'type' => 'I', 'keep' => 0, 'ordernum' => 9 ),
			array( 'tag' => 'IMMI', 'display' => 'Immigration',   'type' => 'I', 'keep' => 0, 'ordernum' => 10 ),
			array( 'tag' => 'NATU', 'display' => 'Naturalisation','type' => 'I', 'keep' => 0, 'ordernum' => 11 ),
			array( 'tag' => 'GRAD', 'display' => 'Graduation',    'type' => 'I', 'keep' => 0, 'ordernum' => 12 ),
			array( 'tag' => 'RETI', 'display' => 'Retirement',    'type' => 'I', 'keep' => 0, 'ordernum' => 13 ),
			array( 'tag' => 'WILL', 'display' => 'Will',          'type' => 'I', 'keep' => 0, 'ordernum' => 14 ),
			array( 'tag' => 'PROB', 'display' => 'Probate',       'type' => 'I', 'keep' => 0, 'ordernum' => 15 ),
			array( 'tag' => 'OCCU', 'display' => 'Occupation',    'type' => 'I', 'keep' => 0, 'ordernum' => 16 ),
			array( 'tag' => 'RESI', 'display' => 'Residence',     'type' => 'I', 'keep' => 0, 'ordernum' => 17 ),
			array( 'tag' => 'EDUC', 'display' => 'Education',     'type' => 'I', 'keep' => 0, 'ordernum' => 18 ),
			array( 'tag' => 'MARR', 'display' => 'Marriage',      'type' => 'F', 'keep' => 1, 'ordernum' => 19 ),
			array( 'tag' => 'DIV',  'display' => 'Divorce',       'type' => 'F', 'keep' => 1, 'ordernum' => 20 ),
			array( 'tag' => 'ENGA', 'display' => 'Engagement',    'type' => 'F', 'keep' => 0, 'ordernum' => 21 ),
			array( 'tag' => 'MARC', 'display' => 'Marriage Contract','type'=>'F','keep'=> 0, 'ordernum' => 22 ),
		);

		$now = Tables::now();
		foreach ( $core_types as $type ) {
			// Skip if already exists.
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$exists = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE tag = %s AND type = %s', self::table(), $type['tag'], $type['type'] ) );
			if ( $exists ) {
				continue;
			}
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->insert( self::table(), array_merge( $type, array(
				'created_by_user_id' => 0,
				'created_at'         => $now,
				'updated_at'         => $now,
			) ) );
		}
	}
}
