<?php

namespace RadiusTheme\RadiusBooking\ExportImport\Importers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use RadiusTheme\RadiusBooking\ExportImport\Contracts\ImporterInterface;
use RadiusTheme\RadiusBooking\ExportImport\IdMapper;
use RadiusTheme\RadiusBooking\Models\Appointment;
use RadiusTheme\RadiusBooking\Models\CustomerBooking;
use RadiusTheme\RadiusBooking\Models\Payment;
use RadiusTheme\RadiusBooking\Models\AppointmentIntegration;

/**
 * Class AppointmentImporter
 *
 * Imports appointments with bookings, payments, integrations, and full ID remapping.
 *
 * @package RadiusTheme\RadiusBooking\ExportImport\Importers
 */
class AppointmentImporter implements ImporterInterface {

	/**
	 * {@inheritDoc}
	 */
	public function getModuleName(): string {
		return 'appointment';
	}

	/**
	 * {@inheritDoc}
	 */
	public function import( array $records, IdMapper $id_mapper ): int {
		$imported = 0;

		foreach ( $records as $record ) {
			$old_id       = (int) ( $record['id'] ?? 0 );
			$bookings     = $record['bookings'] ?? array();
			$integrations = $record['integrations'] ?? array();

			unset( $record['id'], $record['bookings'], $record['integrations'] );
			unset( $record['created_at'], $record['updated_at'], $record['deleted_at'] );

			// Remap foreign keys.
			if ( ! empty( $record['employee_id'] ) ) {
				$new_emp_id = $id_mapper->getNewId( 'staff', (int) $record['employee_id'] );
				if ( $new_emp_id ) {
					$record['employee_id'] = $new_emp_id;
				}
			}

			if ( ! empty( $record['service_id'] ) ) {
				$new_svc_id = $id_mapper->getNewId( 'service', (int) $record['service_id'] );
				if ( $new_svc_id ) {
					$record['service_id'] = $new_svc_id;
				}
			}

			if ( ! empty( $record['location_id'] ) ) {
				$new_loc_id = $id_mapper->getNewId( 'location', (int) $record['location_id'] );
				if ( $new_loc_id ) {
					$record['location_id'] = $new_loc_id;
				}
			}

			// Remap parent_id for recurring appointments.
			if ( ! empty( $record['parent_id'] ) ) {
				$new_parent_id = $id_mapper->getNewId( 'appointment', (int) $record['parent_id'] );
				if ( $new_parent_id ) {
					$record['parent_id'] = $new_parent_id;
				}
			}

			$appointment = Appointment::create( $record );
			if ( ! $appointment ) {
				continue;
			}

			$new_appt_id = (int) $appointment->id;
			if ( $old_id ) {
				$id_mapper->addMapping( 'appointment', $old_id, $new_appt_id );
			}

			// Import bookings.
			foreach ( $bookings as $booking_data ) {
				$payment_data = $booking_data['payment'] ?? null;
				unset( $booking_data['payment'], $booking_data['created_at'], $booking_data['updated_at'] );
				$booking_data['appointment_id'] = $new_appt_id;

				// Remap customer_id.
				if ( ! empty( $booking_data['customer_id'] ) ) {
					$new_cust_id = $id_mapper->getNewId( 'customer', (int) $booking_data['customer_id'] );
					if ( $new_cust_id ) {
						$booking_data['customer_id'] = $new_cust_id;
					}
				}

				$booking = CustomerBooking::create( $booking_data );
				if ( ! $booking ) {
					continue;
				}

				$new_booking_id = (int) $booking->id;

				// Import payment.
				if ( $payment_data ) {
					unset( $payment_data['created_at'], $payment_data['updated_at'] );
					$payment_data['booking_id'] = $new_booking_id;

					// Remap customer_id in payment.
					if ( ! empty( $payment_data['customer_id'] ) ) {
						$new_cust_id = $id_mapper->getNewId( 'customer', (int) $payment_data['customer_id'] );
						if ( $new_cust_id ) {
							$payment_data['customer_id'] = $new_cust_id;
						}
					}

					// Clear site-specific payment data.
					$payment_data['transaction_id'] = null;

					Payment::create( $payment_data );
				}
			}

			// Import integrations.
			foreach ( $integrations as $int_data ) {
				unset( $int_data['created_at'], $int_data['updated_at'] );
				$int_data['appointment_id'] = $new_appt_id;

				// Clear external IDs (won't be valid on new site).
				$int_data['external_event_id']    = null;
				$int_data['external_calendar_id'] = null;

				AppointmentIntegration::create( $int_data );
			}

			++$imported;
		}

		return $imported;
	}

	/**
	 * {@inheritDoc}
	 */
	public function getDependencies(): array {
		return array( 'staff', 'service', 'customer', 'location' );
	}
}
