<?php

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

class WPDD_Roles {
    
    public static function init() {
        // Call immediately since we're already in init hook
        self::maybe_create_roles();
    }
    
    public static function maybe_create_roles() {
        if (get_option('wpdd_roles_created') !== WPDD_VERSION) {
            self::create_roles();
            update_option('wpdd_roles_created', WPDD_VERSION);
        }
    }
    
    public static function create_roles() {
        self::create_customer_role();
        self::create_creator_role();
        self::add_admin_capabilities();
    }
    
    private static function create_customer_role() {
        add_role(
            'wpdd_customer',
            __('Digital Customer', 'wp-digital-download'),
            array(
                'read' => true,
                'wpdd_view_purchases' => true,
                'wpdd_download_products' => true,
            )
        );
    }
    
    private static function create_creator_role() {
        add_role(
            'wpdd_creator',
            __('Digital Creator', 'wp-digital-download'),
            array(
                'read' => true,
                'upload_files' => true,
                'edit_posts' => false,
                'delete_posts' => false,
                'publish_posts' => false,
                
                'edit_wpdd_products' => true,
                'edit_published_wpdd_products' => true,
                'publish_wpdd_products' => true,
                'delete_wpdd_products' => true,
                'delete_published_wpdd_products' => true,
                'edit_private_wpdd_products' => true,
                'delete_private_wpdd_products' => true,
                
                'wpdd_view_own_sales' => true,
                'wpdd_manage_own_products' => true,
                'wpdd_upload_product_files' => true,
                'wpdd_view_reports' => true,
            )
        );
    }
    
    private static function add_admin_capabilities() {
        $role = get_role('administrator');
        
        if ($role) {
            $role->add_cap('edit_wpdd_products');
            $role->add_cap('edit_others_wpdd_products');
            $role->add_cap('edit_published_wpdd_products');
            $role->add_cap('publish_wpdd_products');
            $role->add_cap('delete_wpdd_products');
            $role->add_cap('delete_others_wpdd_products');
            $role->add_cap('delete_published_wpdd_products');
            $role->add_cap('edit_private_wpdd_products');
            $role->add_cap('delete_private_wpdd_products');
            
            $role->add_cap('wpdd_manage_settings');
            $role->add_cap('wpdd_view_all_sales');
            $role->add_cap('wpdd_manage_all_products');
            $role->add_cap('wpdd_view_reports');
            $role->add_cap('wpdd_manage_orders');
        }
    }
    
    public static function remove_roles() {
        remove_role('wpdd_customer');
        remove_role('wpdd_creator');
        
        $role = get_role('administrator');
        if ($role) {
            $caps = array(
                'edit_wpdd_products',
                'edit_others_wpdd_products',
                'edit_published_wpdd_products',
                'publish_wpdd_products',
                'delete_wpdd_products',
                'delete_others_wpdd_products',
                'delete_published_wpdd_products',
                'edit_private_wpdd_products',
                'delete_private_wpdd_products',
                'wpdd_manage_settings',
                'wpdd_view_all_sales',
                'wpdd_manage_all_products',
                'wpdd_view_reports',
                'wpdd_manage_orders'
            );
            
            foreach ($caps as $cap) {
                $role->remove_cap($cap);
            }
        }
    }
}