<?php
declare(strict_types=1);

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

final class RC_License {

	public static function get_mode(): string {
		if ( defined( 'RC_LICENSE_OVERRIDE' ) ) {
			$override = RC_LICENSE_OVERRIDE;
			if ( 'demo' === $override || 'full' === $override ) {
				return $override;
			}
		}

		$value = defined( 'RC_Build_Hash::VALUE' ) ? RC_Build_Hash::VALUE : '';

		if ( '' === $value ) {
			return 'full';
		}

		if ( $value === self::_build( 'd', 'm' ) ) {
			return 'demo';
		}
		return 'full';
	}

	public static function is_demo(): bool {
		return 'demo' === self::get_mode();
	}

	public static function get_affiliate_id(): string {
		if ( ! defined( 'RC_Build_Hash::AFFILIATE_ID' ) ) {
			return '';
		}
		$id = RC_Build_Hash::AFFILIATE_ID;
		return is_string( $id ) && '' !== $id ? $id : '';
	}

	public static function get_download_site(): string {
		if ( ! defined( 'RC_Build_Hash::DOWNLOAD_SITE' ) ) {
			return '';
		}
		$site = RC_Build_Hash::DOWNLOAD_SITE;
		return is_string( $site ) && '' !== $site ? $site : '';
	}

	public static function is_affiliate_demo(): bool {
		return self::is_demo() && '' !== self::get_affiliate_id();
	}

	public static function get_licensed_domain(): string {
		if ( defined( 'RC_LICENSED_DOMAIN' ) ) {
			return RC_LICENSED_DOMAIN;
		}
		return '';
	}

	public static function is_domain_match(): bool {
		$licensed = self::get_licensed_domain();
		if ( '' === $licensed ) {
			return true;
		}
		$current = wp_parse_url( home_url(), PHP_URL_HOST );
		return $current && strtolower( $current ) === strtolower( $licensed );
	}

	public static function can_create_ingredient(): bool {
		if ( ! self::is_demo() ) {
			return true;
		}
		return RC_Ingredient_Repository::count() < self::_max( 'ingredient' );
	}

	public static function can_create_recipe( int $editing_id = 0 ): bool {
		if ( ! self::is_demo() ) {
			return true;
		}
		if ( $editing_id > 0 ) {
			$editing = RC_Recipe_Repository::get( $editing_id );
			if ( $editing ) {
				return true;
			}
		}
		return RC_Recipe_Repository::count() < self::_max( 'recipe' );
	}

	public static function should_watermark(): bool {
		return self::is_demo();
	}

	public static function caps(): array {
		return array(
			'ingredients' => self::is_demo() ? self::_max( 'ingredient' ) : -1,
			'recipes'     => self::is_demo() ? self::_max( 'recipe' ) : -1,
		);
	}

	public static function over_cap_counts(): array {
		if ( ! self::is_demo() ) {
			return array();
		}
		$ingredients = RC_Ingredient_Repository::count();
		$recipes     = RC_Recipe_Repository::count();
		$over        = array();
		if ( $ingredients > self::_max( 'ingredient' ) ) {
			$over['ingredients'] = $ingredients - self::_max( 'ingredient' );
		}
		if ( $recipes > self::_max( 'recipe' ) ) {
			$over['recipes'] = $recipes - self::_max( 'recipe' );
		}
		return $over;
	}

	private static function _max( string $type ): int {
		if ( 'ingredient' === $type ) {
			return 50;
		}
		if ( 'recipe' === $type ) {
			return 10;
		}
		return 0;
	}

	private static function _build( string $prefix, string $suffix ): string {
		$salt = defined( 'RC_LICENSE_SALT' ) ? RC_LICENSE_SALT : 'rc7';
		$key  = $prefix . $salt . $suffix;
		return substr( sha1( $key ), 0, 16 );
	}
}
