<?php

namespace Booktics\Appointment;

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

use Booktics\Contracts\Hookable_Service_Contract;

class Appointment_Hooks implements Hookable_Service_Contract {

    /**
     * Register the appointment hooks.
     *
     * @return void
     */
    public function register() {
        add_filter( 'booktics_appointment_args', array(
            $this,
            'booktics_appointment_args'
        ) );
    }

    /**
     * Filter appointment arguments based on the current user's role.
     *
     * @param array $args The appointment arguments.
     *
     * @return array The filtered appointment arguments.
     */
    public function booktics_appointment_args( $args ) {
        $current_user = wp_get_current_user();
        $roles        = $current_user->roles;

        if ( in_array( 'administrator', $roles, true ) ) {
            return $args;
        }

        if ( in_array( 'booktics_team_member', $roles, true ) ) {
            if ( ! isset( $args['meta_query'] ) ) {
                // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering team members by meta fields
                $args['meta_query'] = [];
            }
            $args['meta_query'][] = array(
                'key'     => 'team_member_id',
                'value'   => $current_user->ID,
                'compare' => '=',
            );
        }

        return $args;
    }
}