<?php

namespace Booktics\Team_Member\Services;

use Booktics\Abstracts\Booktics_Database;
use Booktics\Services\Schedule_Record;
use DateTime;
use Exception;

class Team_Member_Availability {
    /**
     * Store team member id
     *
     * @var string|int
     */
    private $team_member;

    /**
     * Booktics database object
     *
     * @var Booktics_Database
     */
    private $db;

    /**
     * Booktics schedule table name
     *
     * @var string
     */
    private $table_name;

    /**
     * Init team member and db
     *
     * @param $team_member
     */
    public function __construct( $team_member ) {
        $this->team_member = $team_member->to_array();
        $this->db          = new Booktics_Database();
        $this->table_name  = booktics_get_table_name( 'booktics_schedules' );
    }

    /**
     * @param $start_date
     * @param $days
     *
     * @return array
     * @throws Exception
     */
    public function get_weekly_schedule( $start_date, $days = 7 ): array {
        // If team member status is pending, return empty array
        if ( $this->team_member['status'] === 'pending' ) {
            return array();
        }

        $results = array();
        $date    = new DateTime( $start_date );

        for ( $i = 0; $i < $days; $i++ ) {
            $date_str  = $date->format( 'Y-m-d' );
            $status    = $this->get_status( $date_str );
            $results[] = array(
                'date'   => $date_str,
                'day'    => $date->format( 'D' ),
                'status' => $status,
            );
            $date->modify( '+1 day' );
        }

        return $results;
    }

    /**
     * Get status for a given date
     *
     * @param string $date Date in Y-m-d format
     *
     * @return string
     */
    public function get_status( $date ): string {
        // If team member status is pending, return 'pending'
        if ( $this->team_member['status'] === 'pending' ) {
            return 'pending';
        }

        if ( ! $this->team_member['enable_custom_availability'] ) {
            return $this->get_default_workday_status( $date );
        }

        // Get all schedules for the team member
        $schedules = (new Schedule_Record())->get_schedules( $this->team_member['id'] );

        // If no schedules exist, it's a holiday
        if ( empty( $schedules ) || ! isset( $schedules['schedule'] ) ) {
            return 'holiday';
        }

        $date_obj   = new DateTime( $date );
        $day_name   = strtolower( $date_obj->format( 'D' ) );
        $date_str   = $date_obj->format( 'Y-m-d' );

        // Check regular schedule
        if ( ! empty( $schedules['schedule'][ $day_name ] ) ) {
            $day_slots = $schedules['schedule'][ $day_name ];
            // If there are time slots, it's a working day
            if ( ! empty( $day_slots ) ) {
                return 'working';
            }
        }

        // Check holidays
        if ( ! empty( $schedules['holiday'] ) ) {
            foreach ( $schedules['holiday'] as $holiday_date => $holiday_data ) {
                if ( $holiday_date === $date_str ) {
                    return 'holiday';
                }
            }
        }

        // If no schedule is found for this day, it's a holiday
        return 'holiday';
    }

    private function get_default_workday_status( $date ) {
        $date_obj   = new DateTime( $date );
        $day_name   = strtolower( $date_obj->format( 'D' ) );
        $availability = booktics_get_option( 'schedule' ) ?? array(); 
        
        // Ensure availability is always an array
        if ( ! is_array( $availability ) ) {
            $availability = array();
        }
        
        // schedule keys in settings are default working days from settings
        $default_working_days = array_keys( $availability );

        if ( ! in_array( $day_name, $default_working_days ) ) {
            return 'holiday';
        }

        return 'working';
    }

}
