<?php

namespace SugarCalendarBookings\Locations;

/**
 * Location logic for Location model.
 *
 * @since 1.0.0
 */
class LocationService {

	/**
	 * Create a new Location.
	 *
	 * @since 1.0.0
	 *
	 * @param CreateLocationDTO $dto Data for location creation.
	 *
	 * @return LocationModel|false The created Location model or false on failure.
	 */
	public function create( $dto ) {

		$location = new LocationModel();

		$this->fill_location( $location, $dto );

		$saved = $location->save();

		if ( ! $saved ) {
			return false;
		}

		return $location;
	}

	/**
	 * Update an existing Location.
	 *
	 * @since 1.0.0
	 *
	 * @param LocationModel $location The location model to update.
	 * @param LocationDTO   $dto      Data for location update.
	 *
	 * @return LocationModel|false The updated Location model or false on failure.
	 */
	public function update( $location, $dto ) {

		$this->fill_location( $location, $dto );

		$saved = $location->save();

		if ( ! $saved ) {
			return false;
		}

		return $location;
	}

	/**
	 * Delete a Location.
	 *
	 * @since 1.0.0
	 *
	 * @param LocationModel $location The location model to delete.
	 *
	 * @return bool
	 */
	public function delete( $location ) {

		return $location->delete();
	}

	/**
	 * Get the first enabled location.
	 *
	 * Returns the first enabled location ordered by ID (the default/oldest location).
	 *
	 * @since 1.2.0
	 *
	 * @return LocationModel|null Location model if exists, null otherwise.
	 */
	public function get_first_enabled() {

		return LocationModel::query()
			->where( 'status', '=', 'enabled', '%s' )
			->order_by( 'id' )
			->first();
	}

	/**
	 * Get the first enabled location ID.
	 *
	 * @since 1.2.0
	 *
	 * @return int|null Location ID if exists, null otherwise.
	 */
	public function get_first_enabled_location_id() {

		$location = $this->get_first_enabled();

		return $location ? $location->id : null;
	}

	/**
	 * Fill a Location model from DTO.
	 *
	 * @since 1.0.0
	 *
	 * @param LocationModel $location The location model to fill.
	 * @param LocationDTO   $dto      Data transfer object.
	 */
	protected function fill_location( $location, $dto ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded

		if ( isset( $dto->name ) ) {
			$location->name = $dto->name;
		}

		if ( isset( $dto->address ) ) {
			$location->address = $dto->address;
		}

		if ( isset( $dto->address2 ) ) {
			$location->address2 = $dto->address2;
		}

		if ( isset( $dto->city ) ) {
			$location->city = $dto->city;
		}

		if ( isset( $dto->region ) ) {
			$location->region = $dto->region;
		}

		if ( isset( $dto->postal_code ) ) {
			$location->postal_code = $dto->postal_code;
		}

		if ( isset( $dto->country_code ) ) {
			$location->country_code = $dto->country_code;
		}

		if ( isset( $dto->latitude ) ) {
			$location->latitude = $dto->latitude;
		}

		if ( isset( $dto->longitude ) ) {
			$location->longitude = $dto->longitude;
		}

		if ( isset( $dto->timezone ) ) {
			$location->timezone = $dto->timezone;
		}

		if ( isset( $dto->phone ) ) {
			$location->phone = $dto->phone;
		}

		if ( isset( $dto->website ) ) {
			$location->website = $dto->website;
		}
	}
}
