<?php

namespace WeDevs\PM\Core\WP;

use WeDevs\PM\Core\WP\Output as Output;
use WeDevs\PM\Core\WP\Enqueue_Scripts as Enqueue_Scripts;
use WeDevs\PM\User\Models\User;
use WeDevs\PM\User\Models\User_Role;

class Menu {

	private static $capability = 'read';

    public static function admin_menu() {
        global $submenu, $wedevs_pm_pro, $wedevs_license_progress;

        $slug = wedevs_pm_admin_slug();

        $home = add_menu_page( __( 'Project Manager', 'wedevs-project-manager' ), __( 'Project Manager', 'wedevs-project-manager' ), self::$capability, $slug, array( new Output, 'home_page' ), self::pm_svg(), 3 );

        // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
        // 1. Projects
        $submenu[$slug][] = [ __( 'Projects', 'wedevs-project-manager' ), self::$capability, "admin.php?page={$slug}#/" ];

        // 2. My Tasks
        $active_task = self::my_task_count();
        // translators: %s: pending task count number
        $mytask_text = sprintf( __( 'My Tasks %s', 'wedevs-project-manager' ), '<span class="awaiting-mod count-1"><span class="pending-count">' . $active_task . '</span></span>' );
        // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
        $submenu[$slug][] = [ $mytask_text, self::$capability, "admin.php?page={$slug}#/my-tasks" ];

        // 3. Views + Modules: Calendar, Progress, Reports, Modules, Sprints, Woo Project
        //    — added by pro plugin via wedevs_pm_menu_before_load_scripts
        do_action( 'wedevs_pm_menu_before_load_scripts', $home );

        add_action( 'admin_print_styles-' . $home, array( 'WeDevs\\PM\\Core\\WP\\Menu', 'scripts' ) );

        do_action( 'wedevs_cpm_admin_menu', self::$capability, $home );

        // 4. Upgrade to Pro (free only)
        if ( ! $wedevs_pm_pro ) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
            $submenu[$slug][] = [ __( 'Premium', 'wedevs-project-manager' ), self::$capability, "admin.php?page={$slug}#/premium" ];
        }

        // 5. Administration: Categories, Settings, Tools
        if ( wedevs_pm_user_can_access( wedevs_pm_manager_cap_slug() ) ) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
            $submenu[$slug][] = [ __( 'Categories', 'wedevs-project-manager' ), self::$capability, "admin.php?page={$slug}#/categories" ];
        }

        if ( wedevs_pm_has_admin_capability() ) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
            $submenu[$slug][] = [ __( 'Settings', 'wedevs-project-manager' ), self::$capability, "admin.php?page={$slug}#/settings" ];
        }

        if ( wedevs_pm_user_can_access( wedevs_pm_manager_cap_slug() ) ) {
            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentionally adding custom submenu items to WordPress admin menu
            $submenu[$slug]['importtools'] = [ __( 'Tools', 'wedevs-project-manager' ), self::$capability, "admin.php?page={$slug}#/importtools" ];
        }

        // 6. License — added by pm-pro via wedevs_pm_menu_after_load_scripts
        do_action( 'wedevs_pm_menu_after_load_scripts', $home );
    }

	public static function pm_svg() {
		return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMThweCIgaGVpZ2h0PSIxOXB4IiB2aWV3Qm94PSIwIDAgMTggMTkiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUyLjMgKDY3Mjk3KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5JY29uIHdoaXRlPC90aXRsZT4KICAgIDxkZXNjPkNyZWF0ZWQgd2l0aCBTa2V0Y2guPC9kZXNjPgogIC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NzUyODksNC41NTIxNDY5MyAxNy44MTU5Mzc1LDcuNzkyNDc4MzQgQzE4LjM3NDM0NiwxMS4wMzI4MDk4IDE3LjIxOTAyOTUsMTQuMzM0Mzk2MiAxNC43OCwxNi40NjgzNzg0IEwxNC43NiwxNi40NjMyNDMyIFoiIGlkPSJTaGFwZSI+PC9wYXRoPgogICAgICAgICAgICAgICAgPC9nPgogICAgICAgICAgICA8L2c+CiAgICAgICAgPC9nPgogICAgICAgIDxnIGlkPSJQYWdlIj48L2c+CiAgICA8L2c+Cjwvc3ZnPgoK';
	}

	public static function scripts() {
		Enqueue_Scripts::scripts();
		Enqueue_Scripts::styles();
	}

	public static function my_task_count () {
		$today = gmdate( 'Y-m-d', strtotime( current_time( 'mysql' ) ) );
		$user_id = get_current_user_id();

		$project_ids = User_Role::where( 'user_id', $user_id)->get(['project_id'])->toArray();
        $project_ids = wp_list_pluck( $project_ids, 'project_id' );

        if ( wedevs_pm_has_manage_capability() ){
            $tasks = User::find( $user_id )
            	->tasks()
                ->whereHas('boards')
                ->parent()
                ->where( wedevs_pm_tb_prefix() . 'pm_tasks.status', 0)
            	->whereIn( wedevs_pm_tb_prefix() . 'pm_tasks.project_id', $project_ids)
                ->get();
        }else{
            $tasks = User::find( $user_id )->tasks()
                ->parent()
                ->where( wedevs_pm_tb_prefix() . 'pm_tasks.status', 0)
                ->whereIn( wedevs_pm_tb_prefix() . 'pm_tasks.project_id', $project_ids)
                ->doesntHave( 'metas', 'and', function ($query) {
                    $query->where( 'meta_key', '=', 'privacy' )
                        ->where( 'meta_value', '!=', '0' );

                });

            $tasks = $tasks->doesntHave( 'task_lists.metas', 'and', function ($query) {
                $query->where( 'meta_key', '=', 'privacy' )
                        ->where( 'meta_value', '!=', '0' );

                })->get();
        }

        return $tasks->count();
	}
}
