<?php
/**
 * Licensed Variable Product Class
 *
 * @package Jeremias\WcLicensedProduct\Product
 */

declare(strict_types=1);

namespace Jeremias\WcLicensedProduct\Product;

use Jeremias\WcLicensedProduct\Admin\SettingsController;
use WC_Product_Variable;

/**
 * Licensed Variable Product type extending WooCommerce Variable Product
 *
 * This allows selling license subscriptions with different durations
 * (e.g., monthly, yearly, lifetime) as product variations.
 */
class LicensedVariableProduct extends WC_Product_Variable
{
    /**
     * Product type
     */
    protected $product_type = 'licensed-variable';

    /**
     * Constructor
     */
    public function __construct($product = 0)
    {
        parent::__construct($product);
    }

    /**
     * Get product type
     */
    public function get_type(): string
    {
        return 'licensed-variable';
    }

    /**
     * Licensed products are always virtual
     */
    public function is_virtual(): bool
    {
        return true;
    }

    /**
     * Licensed products are purchasable
     */
    public function is_purchasable(): bool
    {
        return $this->exists() && $this->get_price() !== '';
    }

    /**
     * Get max activations for this product (parent default)
     * Falls back to default settings if not set on product
     */
    public function get_max_activations(): int
    {
        $value = $this->get_meta('_licensed_max_activations', true);
        if ($value !== '' && $value !== null) {
            return max(1, (int) $value);
        }
        return SettingsController::getDefaultMaxActivations();
    }

    /**
     * Check if product has custom max activations set
     */
    public function has_custom_max_activations(): bool
    {
        $value = $this->get_meta('_licensed_max_activations', true);
        return $value !== '' && $value !== null;
    }

    /**
     * Get validity days (parent default - variations override this)
     * Falls back to default settings if not set on product
     */
    public function get_validity_days(): ?int
    {
        $value = $this->get_meta('_licensed_validity_days', true);
        if ($value !== '' && $value !== null) {
            return (int) $value > 0 ? (int) $value : null;
        }
        return SettingsController::getDefaultValidityDays();
    }

    /**
     * Check if product has custom validity days set
     */
    public function has_custom_validity_days(): bool
    {
        $value = $this->get_meta('_licensed_validity_days', true);
        return $value !== '' && $value !== null;
    }

    /**
     * Check if license should be bound to major version
     * Falls back to default settings if not set on product
     */
    public function is_bound_to_version(): bool
    {
        $value = $this->get_meta('_licensed_bind_to_version', true);
        if ($value !== '' && $value !== null) {
            return $value === 'yes';
        }
        return SettingsController::getDefaultBindToVersion();
    }

    /**
     * Check if product has custom bind to version setting
     */
    public function has_custom_bind_to_version(): bool
    {
        $value = $this->get_meta('_licensed_bind_to_version', true);
        return $value !== '' && $value !== null;
    }

    /**
     * Get current software version (derived from latest product version)
     */
    public function get_current_version(): string
    {
        $versionManager = new VersionManager();
        $latestVersion = $versionManager->getLatestVersion($this->get_id());

        return $latestVersion ? $latestVersion->getVersion() : '';
    }

    /**
     * Get major version number from version string
     */
    public function get_major_version(): int
    {
        $versionManager = new VersionManager();
        $latestVersion = $versionManager->getLatestVersion($this->get_id());

        if ($latestVersion) {
            return $latestVersion->getMajorVersion();
        }

        return 1;
    }
}
