<?php

namespace SugarCalendarBookings\Employees;

use SugarCalendarBookings\Services\ServiceModel;
use SugarCalendarBookings\Appointments\AppointmentModel;
use SugarCalendarBookings\Support\Database\AbstractModel;
use SugarCalendarBookings\Availability\WorkingScheduleDefaults;
use SugarCalendarBookings\Support\Database\Relations\HasManyRelation;
use SugarCalendarBookings\Support\Database\Relations\BelongsToManyRelation;

/**
 * Employee model.
 *
 * @since 1.0.0
 *
 * @property int    $id
 * @property string $name
 * @property string $status
 * @property string $email
 * @property string $phone
 * @property int    $user_id
 * @property bool   $use_default_working_schedule
 * @property array  $working_schedule
 * @property string $created_at
 * @property string $updated_at
 */
class EmployeeModel extends AbstractModel {

	/**
	 * The database table name without prefix.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	protected static $table_name = 'employees';

	/**
	 * The model type identifier.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	protected static $type = 'employee';

	/**
	 * Column formats for the model.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $column_formats = [
		'id'         => '%d',
		'name'       => '%s',
		'status'     => '%s',
		'email'      => '%s',
		'phone'      => '%s',
		'user_id'    => '%d',
		'created_at' => '%s',
		'updated_at' => '%s',
	];

	/**
	 * Meta keys for the model.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, mixed>
	 */
	protected static $meta_keys = [
		'use_default_working_schedule',
		'working_schedule',
		'_amelia_id',
	];

	/**
	 * The attributes and meta that should be cast.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $casts = [
		'id'                           => 'integer',
		'use_default_working_schedule' => 'boolean',
		'working_schedule'             => 'array',
		'created_at'                   => 'datetime',
		'updated_at'                   => 'datetime',
	];

	/**
	 * Column defaults for the model.
	 *
	 * @since 1.0.0
	 *
	 * @return array<string, mixed>
	 */
	protected static function column_defaults() {

		return [
			'status' => 'enabled',
		];
	}

	/**
	 * Meta defaults for the model.
	 *
	 * @since 1.0.0
	 *
	 * @return array<string, mixed>
	 */
	protected static function meta_defaults() {

		return [
			'use_default_working_schedule' => true,
			'working_schedule'             => WorkingScheduleDefaults::to_array(),
		];
	}

	/**
	 * Get the appointments relationship for the employee.
	 *
	 * @since 1.0.0
	 *
	 * @return HasManyRelation
	 */
	public function appointments() {

		return $this->has_many( AppointmentModel::class );
	}

	/**
	 * Get the services relationship for the employee.
	 *
	 * @since 1.0.0
	 *
	 * @return BelongsToManyRelation
	 */
	public function services() {

		return $this->belongs_to_many( ServiceModel::class );
	}

	/**
	 * Get or create a default employee.
	 *
	 * Used during migration when migrating to SCB Lite which doesn't
	 * import employees from Amelia.
	 *
	 * @since 1.3.0
	 *
	 * @return EmployeeModel|null
	 */
	public static function get_or_create_default(): ?EmployeeModel {

		// Try to find existing enabled employee.
		$existing = self::query()
			->where( 'status', '=', 'enabled', '%s' )
			->order_by( 'id', 'ASC' )
			->first();

		if ( $existing ) {
			return $existing;
		}

		// Get current user info.
		$user  = wp_get_current_user();
		$name  = __( 'Owner', 'sugar-calendar-bookings-scheduling-appointments-lite' );
		$email = get_option( 'admin_email' );

		if ( $user->ID > 0 ) {
			$name  = ! empty( $user->first_name ) ? $user->first_name : $user->display_name;
			$email = $user->user_email;
		}

		$employee = new self(
			[
				'name'       => $name,
				'email'      => $email,
				'status'     => 'enabled',
				'created_at' => current_time( 'mysql' ),
				'updated_at' => current_time( 'mysql' ),
			]
		);

		if ( $employee->save() ) {
			$employee->set_meta( '_migration_default', true );

			return $employee;
		}

		return null;
	}
}
