<?php

namespace Booktics;

class Role {

    /**
     * Initialize roles and set them for users
     *
     * @return void
     */
    public function init() {
        $this->register_role();
        $this->set_role();
    }

    /**
     * Register role for team member
     *
     * @return void
     */
    public function register_role() {
        $roles = $this->get_roles();

        foreach ( $roles as $role ) {
            add_role(
                $role['name'],
                $role['display_name'],
                $role['capabilities']
            );
        }
    }

    /**
     * Get roles
     *
     * @return  array
     */
    public function get_roles() {
        $roles = array(
            array(
                'name'         => 'booktics_team_member',
                'display_name' => esc_html__( 'Team Member', 'booktics' ),
                'capabilities' => array(
                    'read'                   => true,
                    'delete_posts'           => true,
                    // 'delete_published_posts' => true,
                    'edit_posts'             => true,
                    'publish_posts'          => true,
                    'upload_files'           => true,
                    'edit_pages'             => true,
                    'edit_published_pages'   => true,
                    'publish_pages'          => true,
                    // 'delete_published_pages' => true,
                    'read_booking'           => true,
                    'read_meeting'           => true,
                    'edit_profile'           => true,
                    'manage_options'         => true,

                ),
            ),
            array(
                'name'         => 'booktics_customer',
                'display_name' => esc_html__( 'Booktics Customer', 'booktics' ),
                'capabilities' => array(
                    'read'         => true,
                    'edit_profile' => true,
                ),
            ),
        );

        return apply_filters( 'booktics_team_member_roles', $roles );
    }

    /**
     * Set user role
     *
     * @return  void
     */
    public function set_role() {
        $users = get_users( array( 'role' => 'administrator' ) );

        foreach ( $users as $user ) {
            $user->add_role( 'booktics_team_member' );
            $nickname = get_user_meta( $user->ID, 'nickname', true );
            update_user_meta( $user->ID, '_team_member_first_name', $nickname );
            update_user_meta( $user->ID, '_team_member_status', 'active' );
        }
    }
}
