<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class PC_Membership_User_Roles {

    const MEMBER_ROLE_PREFIX = 'pc_member_';

    public static function init() {
        add_action( 'init', array( __CLASS__, 'register_member_roles' ) );
        add_filter( 'editable_roles', array( __CLASS__, 'filter_editable_roles' ) );
        add_action( 'pc_membership_subscription_activated', array( __CLASS__, 'on_subscription_activated' ), 10, 2 );
        add_action( 'pc_membership_subscription_cancelled', array( __CLASS__, 'on_subscription_cancelled' ), 10, 2 );
    }

    public static function register_member_roles() {
        $base_role = get_role( 'subscriber' );

        if ( ! $base_role ) {
            return;
        }

        $capabilities = $base_role->capabilities;

        foreach ( range( 1, 10 ) as $level ) {
            $role_name = self::MEMBER_ROLE_PREFIX . $level;

            if ( ! get_role( $role_name ) ) {
                add_role( $role_name, sprintf( __( 'Member Level %d', 'pc-membership-abc123' ), $level ), $capabilities );
            }
        }
    }

    public static function get_member_role_for_plan( $plan_id ) {
        global $wpdb;

        $plan = $wpdb->get_row( $wpdb->prepare( "SELECT role FROM {$wpdb->prefix}pc_membership_plans WHERE id = %d", $plan_id ) );

        if ( $plan && ! empty( $plan->role ) ) {
            return $plan->role;
        }

        return self::MEMBER_ROLE_PREFIX . '1';
    }

    public static function set_user_member_role( $user_id, $plan_id ) {
        $role = self::get_member_role_for_plan( $plan_id );

        $user = get_userdata( $user_id );

        if ( $user ) {
            $user->set_role( $role );
        }
    }

    public static function remove_member_role( $user_id ) {
        $user = get_userdata( $user_id );

        if ( $user ) {
            $user->set_role( 'subscriber' );
        }
    }

    public static function on_subscription_activated( $user_id, $plan_id ) {
        self::set_user_member_role( $user_id, $plan_id );
    }

    public static function on_subscription_cancelled( $user_id, $plan_id ) {
        self::remove_member_role( $user_id );
    }

    public static function filter_editable_roles( $roles ) {
        foreach ( $roles as $role_key => $role_data ) {
            if ( strpos( $role_key, self::MEMBER_ROLE_PREFIX ) === 0 ) {
                unset( $roles[ $role_key ] );
            }
        }

        return $roles;
    }

    public static function get_plan_for_user( $user_id ) {
        global $wpdb;

        $subscription = $wpdb->get_row( $wpdb->prepare(
            "SELECT plan_id FROM {$wpdb->prefix}pc_membership_subscriptions WHERE user_id = %d AND status = 'active' ORDER BY id DESC LIMIT 1",
            $user_id
        ) );

        if ( ! $subscription ) {
            return null;
        }

        return $subscription->plan_id;
    }

    public static function is_user_on_plan( $user_id, $plan_id ) {
        $user_plan = self::get_plan_for_user( $user_id );
        return $user_plan == $plan_id;
    }

    public static function user_has_active_subscription( $user_id ) {
        global $wpdb;

        $count = $wpdb->get_var( $wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->prefix}pc_membership_subscriptions WHERE user_id = %d AND status = 'active'",
            $user_id
        ) );

        return $count > 0;
    }

    public static function add_member_capabilities() {
        $role = get_role( 'administrator' );

        if ( $role ) {
            $role->add_cap( 'pc_membership_manage' );
            $role->add_cap( 'pc_membership_view_stats' );
        }
    }

    public static function remove_member_capabilities() {
        $role = get_role( 'administrator' );

        if ( $role ) {
            $role->remove_cap( 'pc_membership_manage' );
            $role->remove_cap( 'pc_membership_view_stats' );
        }
    }
}
