<?php

namespace Booktics\Models;

use Booktics\Abstracts\User_Model;
use Booktics\Team_Member\Services\Team_Member_Availability;
use Exception;

class Team_Member_Model extends User_Model {
    /**
     * Store specific user role
     *
     * @var string
     */
    protected $role = 'booktics_team_member';

    /**
     * Store specific meta prefix
     *
     * @var string
     */
    protected $meta_prefix = '_team_member_';

    /**
     * Store user data
     *
     * @var array
     */
    protected $fillable = [
        'id'                     => '',
        'display_name'           => '',
        'first_name'             => '',
        'last_name'              => '',
        'user_email'             => '',
        'image'                  => '',
        'user_login'             => '',
        'phone'                  => '',
        'enable_custom_holiday'  => false,
        'enable_custom_schedule' => false,
        'role'                   => [],
        'status'                 => '',
        'services'               => [],
        'workdays'               => [],
        'schedule'               => [],
        'custom'                 => [],
        'holiday'                => [],
        'booking_count'          => 0,
        'enable_custom_availability' => false,
    ];

    /**
     * Display name attribute
     *
     * @param $team_member
     *
     * @return string
     */
    public function get_display_name_attribute( $team_member ) {
        return $team_member->first_name . ' ' . $team_member->last_name;
    }

    /**
     * Get team member status attribute
     *
     * Returns 'pending' if team member status is not active,
     * otherwise returns current availability status for the current date
     *
     * @param object $team_member
     *
     * @return string Status of the team member ('active'|'pending')
     */
    public function get_status_attribute( $team_member ) {
        return ( new Team_Member_Availability( $team_member ) )
            ->get_status( gmdate( 'Y-m-d' ) );
    }

    /**
     * workdays attribute
     *
     * @param $team_member
     *
     * @return array
     * @throws Exception
     */
    public function get_workdays_attribute( $team_member ) {
        return ( new Team_Member_Availability( $team_member ) )
            ->get_weekly_schedule( gmdate( 'Y-m-d' ) );
    }

    /**
     * email attribute
     *
     * @param $team_member
     *
     * @return string
     */
    public function get_email_attribute( $team_member ) {
        return $team_member->email ?? $team_member->user_email;
    }

    /**
     * Get services attribute
     *
     * @param $team_member
     *
     * @return object
     */
    public function get_schedule_attribute( $team_member ) {
        return (object) ( $team_member->schedule ?? [] );
    }

    /**
     * Get booking count attribute
     *
     * Returns the total count of appointments for this team member
     *
     * @param object $team_member
     *
     * @return int Number of appointments
     */
    public function get_booking_count_attribute( $team_member ) {
        return ( new Appointment_Model() )->get_appointments_count_by_team_member_id( $team_member->id );
    }
}