<?php
namespace OxygenApp\Common\Guards;

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

use WP_REST_Request;
use OxygenApp\Common\Validation\FieldsValidator;
use OxygenApp\Utils\UserUtil;
use OxygenApp\Models\User;
use OxygenApp\Utils\LanguagesHelper;
use OxygenApp\Utils\OptionUtils;

class CreatePlaylistGuard extends BaseGuard implements GuardInterface {

    public static function check(WP_REST_Request $request): array {

        $fieldsRules = [
            [
                'field_name' => 'name',
                'required' => true,
                'type' => 'string',
                'min_length' => 3,
                'max_length' => 100
            ],
            [
                'field_name' => 'sounds',
                'required' => true,
                'type' => 'array',
                'max_length' => 100,
                'item_rules' => [
                    [
                        'field_name' => 'id',
                        'required' => true,
                        'type' => 'int',
                        'max_value' => 1000000000
                    ],
                    [
                        'field_name' => 'soundVolume',
                        'required' => true,
                        'type' => 'int',
                        'min_value' => 0,
                        'max_value' => 100
                    ]
                ]
                ],
                [
                    'field_name' => 'backgroundId',
                    'required' => false,
                    'type' => 'int',
                    'max_value' => 1000000000
                ],

        ];

        $params = $request->get_param('playlist');

        $validatorResult = FieldsValidator::validate($params, $fieldsRules);

        if (!$validatorResult['status']) {
            return self::getFailedResponse($validatorResult['errorMessage'], 400);
        }

        if (!UserUtil::isLoggedIn()) {
            $playlist_login_required = LanguagesHelper::getInstance()->getLangLabel('playlist_login_required');
            return self::getFailedResponse(esc_html($playlist_login_required), 403);
        }

        $userSessionData = UserUtil::getInstance()->getUserFromSession();
        $user_id = $userSessionData['user_id'] ?? '';
        $user = new User();
        $userData = $user->findById($user_id);
        
        if (!$userData) {
            $missing_user = LanguagesHelper::getInstance()->getLangLabel('missing_user');
            return self::getFailedResponse(esc_html($missing_user), 400);
        }

        $soundsNo = sizeof($params['sounds'] ?? []);

        $existingPlaylists = $user->getUserMetaByKey($user_id, 'playlists', []);

        $max_playlists = (int) OptionUtils::getInstance()->getOption('max_playlists_free_users', 10);
        if (sizeof($existingPlaylists) > $max_playlists) {
            $max_playlists_error = LanguagesHelper::getInstance()->getLangLabel('free_user_max_playlists');
            return self::getFailedResponse(esc_html($max_playlists_error), 400);
        }

        $max_sounds_in_playlist = (int) OptionUtils::getInstance()->getOption('max_sounds_in_playlist_free_users', 10);
        if ($soundsNo > $max_sounds_in_playlist) {
            $max_sounds_error = LanguagesHelper::getInstance()->getLangLabel('free_user_max_playlists_sounds');
            return self::getFailedResponse(esc_html($max_sounds_error), 400);
        }

        return self::getSuccessResponse();
    }
}