<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Infrastructure\Database;

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

use Signocore\Multilanguage\Application\Constants;

final class LanguageRepository
{
	/**
	 * @return array<int, object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}>
	 */
	public static function getAll(): array
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		/** @var array<int, object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}> */
		return $wpdb->get_results("SELECT * FROM `{$table}` ORDER BY sort_order ASC, id ASC");
	}

	/**
	 * @return array<int, object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}>
	 */
	public static function getActive(): array
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		/** @var array<int, object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}> */
		return $wpdb->get_results("SELECT * FROM `{$table}` WHERE is_active = 1 ORDER BY sort_order ASC, id ASC");
	}

	/**
	 * @return object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}|null
	 */
	public static function getByCode(string $code): ?object
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$table}` WHERE code = %s", $code));
	}

	/**
	 * @return object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}|null
	 */
	public static function getDefault(): ?object
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->get_row("SELECT * FROM `{$table}` WHERE is_default = 1 LIMIT 1");
	}

	/**
	 * @param array{code: string, locale: string, name: string, native_name: string, is_default?: int, is_active?: int, is_rtl?: int, flag?: string, sort_order?: int, domain?: string|null} $data
	 */
	public static function insert(array $data): int|false
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		$result = $wpdb->insert($table, $data);

		return $result !== false ? $wpdb->insert_id : false;
	}

	/**
	 * @param array<string, mixed> $data
	 */
	public static function update(string $code, array $data): bool
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->update($table, $data, ['code' => $code]) !== false;
	}

	public static function delete(string $code): bool
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->delete($table, ['code' => $code]) !== false;
	}

	public static function clearDefault(): bool
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->update($table, ['is_default' => 0], ['is_default' => 1]) !== false;
	}

	public static function setDefault(string $code): bool
	{
		self::clearDefault();
		return self::update($code, ['is_default' => 1]);
	}

	public static function count(): int
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return (int) $wpdb->get_var("SELECT COUNT(*) FROM `{$table}`");
	}

	/**
	 * @return object{id: int, code: string, locale: string, name: string, native_name: string, is_default: int, is_active: int, is_rtl: int, flag: string, sort_order: int, domain: ?string}|null
	 */
	public static function getByDomain(string $domain): ?object
	{
		global $wpdb;

		$table = $wpdb->prefix . Constants::TABLE_LANGUAGES;

		return $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$table}` WHERE domain = %s AND is_active = 1", $domain));
	}
}
