<?php

namespace SugarCalendarBookings\Locations;

use SugarCalendarBookings\Support\Database\AbstractModel;
use DateTimeImmutable;

/**
 * Location model.
 *
 * @since 1.0.0
 *
 * @property int               $id
 * @property string            $name
 * @property string            $status
 * @property string            $address
 * @property string|null       $address2
 * @property string|null       $city
 * @property string|null       $region
 * @property string|null       $postal_code
 * @property string            $country_code
 * @property float|null        $latitude
 * @property float|null        $longitude
 * @property string|null       $timezone
 * @property string|null       $phone
 * @property string|null       $website
 * @property int               $author_id
 * @property DateTimeImmutable $created_at
 * @property DateTimeImmutable $updated_at
 * @property string            $prev_status
 */
class LocationModel extends AbstractModel {

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

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

	/**
	 * Column formats for the model.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $column_formats = [
		'id'           => '%d',
		'name'         => '%s',
		'status'       => '%s',
		'address'      => '%s',
		'address2'     => '%s',
		'city'         => '%s',
		'region'       => '%s',
		'postal_code'  => '%s',
		'country_code' => '%s',
		'latitude'     => '%f',
		'longitude'    => '%f',
		'timezone'     => '%s',
		'phone'        => '%s',
		'website'      => '%s',
		'author_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 = [
		'prev_status',
		'_amelia_id',
	];

	/**
	 * The attributes and meta that should be cast.
	 *
	 * @since 1.0.0
	 *
	 * @var array<string, string>
	 */
	protected static $casts = [
		'id'         => 'integer',
		'latitude'   => 'float',
		'longitude'  => 'float',
		'author_id'  => 'integer',
		'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',
			'address2' => '',
		];
	}

	/**
	 * Get or create a default location.
	 *
	 * Used during migration when the source doesn't have locations
	 * (e.g., Amelia Lite).
	 *
	 * @since 1.3.0
	 *
	 * @return LocationModel|null
	 */
	public static function get_or_create_default(): ?LocationModel {

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

		if ( $existing ) {
			return $existing;
		}

		// Create default.
		$location = new self(
			[
				'name'       => __( 'Default Location', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'status'     => 'enabled',
				'author_id'  => get_current_user_id() ?: 1,
				'created_at' => current_time( 'mysql' ),
				'updated_at' => current_time( 'mysql' ),
			]
		);

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

			return $location;
		}

		return null;
	}
}
