<?php
/**
 * Livada SEO — Schema Settings View
 *
 * @package Livada_SEO
 */

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

$s = Livada_SEO_Loader::get_settings();

$opening_hours = is_array( $s['schema_opening_hours'] ?? null ) ? $s['schema_opening_hours'] : [];
$same_as       = is_array( $s['schema_same_as'] ?? null )       ? $s['schema_same_as']       : [];
$services      = is_array( $s['schema_services'] ?? null )      ? $s['schema_services']      : [];
$amenities     = is_array( $s['schema_amenities'] ?? null )     ? $s['schema_amenities']     : [];

$schema_type_choices = [
	'LocalBusiness'    => __( 'Local Business (generic)',   'livada-seo' ),
	'Organization'     => __( 'Organization',               'livada-seo' ),
	'Campground'       => __( 'Campground (camping)',       'livada-seo' ),
	'LodgingBusiness'  => __( 'Lodging Business (generic)', 'livada-seo' ),
	'Hotel'            => __( 'Hotel',                      'livada-seo' ),
	'BedAndBreakfast'  => __( 'Bed & Breakfast',            'livada-seo' ),
	'Resort'           => __( 'Resort',                     'livada-seo' ),
	'Hostel'           => __( 'Hostel',                     'livada-seo' ),
	'Motel'            => __( 'Motel',                      'livada-seo' ),
	'Restaurant'       => __( 'Restaurant',                 'livada-seo' ),
	'Store'            => __( 'Store',                      'livada-seo' ),
	'TouristAttraction'=> __( 'Tourist Attraction',         'livada-seo' ),
];

$logo_url = esc_url( $s['schema_logo'] ?? '' );

$days = [
	'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday',
];
?>
<div class="wrap livada-seo-wrap has-ls-hero">
<?php include LIVADA_SEO_PATH . 'includes/admin/views/_partials/header.php'; ?>
	<h1><?php esc_html_e( 'Schema / Structured Data', 'livada-seo' ); ?></h1>

	<?php if ( isset( $_GET['updated'] ) ) : // phpcs:ignore ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'livada-seo' ); ?></p></div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'livada_seo_save_schema' ); ?>
		<input type="hidden" name="action" value="livada_seo_save_schema">

		<!-- ── Business Info ── -->
		<h2><?php esc_html_e( 'LocalBusiness Information', 'livada-seo' ); ?></h2>
		<table class="form-table" role="presentation">
			<tbody>

				<tr>
					<th scope="row"><label for="schema_type"><?php esc_html_e( 'Business Type', 'livada-seo' ); ?></label></th>
					<td>
						<select id="schema_type" name="livada_seo[schema_type]">
							<?php foreach ( $schema_type_choices as $value => $label ) : ?>
								<option value="<?php echo esc_attr( $value ); ?>"<?php selected( $s['schema_type'] ?? 'LocalBusiness', $value ); ?>>
									<?php echo esc_html( $label ); ?>
								</option>
							<?php endforeach; ?>
						</select>
						<p class="description">
							<?php esc_html_e( 'Pick the most specific schema.org type that fits your business. Camping, hotel and lodging types unlock the tourism fields below (rooms, amenities, check-in time, etc.).', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_name"><?php esc_html_e( 'Business Name', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_name" name="livada_seo[schema_name]" value="<?php echo esc_attr( $s['schema_name'] ?? '' ); ?>" class="regular-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_address"><?php esc_html_e( 'Street Address', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_address" name="livada_seo[schema_address]" value="<?php echo esc_attr( $s['schema_address'] ?? '' ); ?>" class="regular-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_city"><?php esc_html_e( 'City', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_city" name="livada_seo[schema_city]" value="<?php echo esc_attr( $s['schema_city'] ?? '' ); ?>" class="regular-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_postal"><?php esc_html_e( 'Postal Code', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_postal" name="livada_seo[schema_postal]" value="<?php echo esc_attr( $s['schema_postal'] ?? '' ); ?>" class="small-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_country"><?php esc_html_e( 'Country Code', 'livada-seo' ); ?></label></th>
					<td>
						<input type="text" id="schema_country" name="livada_seo[schema_country]" value="<?php echo esc_attr( $s['schema_country'] ?? 'FR' ); ?>" class="small-text" maxlength="2" placeholder="FR">
						<p class="description"><?php esc_html_e( 'ISO 3166-1 alpha-2 code (e.g. FR, US, GB).', 'livada-seo' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_phone"><?php esc_html_e( 'Phone', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_phone" name="livada_seo[schema_phone]" value="<?php echo esc_attr( $s['schema_phone'] ?? '' ); ?>" class="regular-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_email"><?php esc_html_e( 'Email', 'livada-seo' ); ?></label></th>
					<td><input type="email" id="schema_email" name="livada_seo[schema_email]" value="<?php echo esc_attr( $s['schema_email'] ?? '' ); ?>" class="regular-text"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_url"><?php esc_html_e( 'Website URL', 'livada-seo' ); ?></label></th>
					<td><input type="url" id="schema_url" name="livada_seo[schema_url]" value="<?php echo esc_url( $s['schema_url'] ?? '' ); ?>" class="regular-text" placeholder="https://"></td>
				</tr>

				<tr>
					<th scope="row"><?php esc_html_e( 'Logo', 'livada-seo' ); ?></th>
					<td>
						<input type="hidden" name="livada_seo[schema_logo]" id="schema_logo" value="<?php echo esc_url( $s['schema_logo'] ?? '' ); ?>">
						<img id="schema_logo_preview" src="<?php echo esc_url( $s['schema_logo'] ?? '' ); ?>" style="max-width:200px;display:block;margin-bottom:8px;<?php echo empty( $s['schema_logo'] ) ? 'display:none;' : ''; ?>" alt="">
						<button type="button" class="button livada-media-upload" data-target="schema_logo"><?php esc_html_e( 'Choose Image', 'livada-seo' ); ?></button>
						<button type="button" class="button livada-media-remove" data-target="schema_logo"<?php echo empty( $s['schema_logo'] ) ? ' style="display:none;"' : ''; ?>><?php esc_html_e( 'Remove', 'livada-seo' ); ?></button>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_geo_lat"><?php esc_html_e( 'Latitude', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_geo_lat" name="livada_seo[schema_geo_lat]" value="<?php echo esc_attr( $s['schema_geo_lat'] ?? '' ); ?>" class="small-text" placeholder="48.8566"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_geo_lng"><?php esc_html_e( 'Longitude', 'livada-seo' ); ?></label></th>
					<td><input type="text" id="schema_geo_lng" name="livada_seo[schema_geo_lng]" value="<?php echo esc_attr( $s['schema_geo_lng'] ?? '' ); ?>" class="small-text" placeholder="2.3522"></td>
				</tr>

				<tr>
					<th scope="row"><label for="schema_price_range"><?php esc_html_e( 'Price Range', 'livada-seo' ); ?></label></th>
					<td>
						<input type="text" id="schema_price_range" name="livada_seo[schema_price_range]" value="<?php echo esc_attr( $s['schema_price_range'] ?? '' ); ?>" class="small-text" placeholder="€€">
						<p class="description"><?php esc_html_e( 'e.g. €, $$, €€€', 'livada-seo' ); ?></p>
					</td>
				</tr>

			</tbody>
		</table>

		<!-- ── Tourism / Lodging fields ── -->
		<h2><?php esc_html_e( 'Tourism / Lodging Fields', 'livada-seo' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'Optional. Most useful when Business Type is Campground, Hotel, BedAndBreakfast, Resort, Hostel or LodgingBusiness. Empty fields are simply omitted from the schema.', 'livada-seo' ); ?>
		</p>
		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><label for="schema_star_rating"><?php esc_html_e( 'Star Rating', 'livada-seo' ); ?></label></th>
					<td>
						<input type="number" min="1" max="5" step="0.5" id="schema_star_rating" name="livada_seo[schema_star_rating]" value="<?php echo esc_attr( $s['schema_star_rating'] ?? '' ); ?>" class="small-text" placeholder="4">
						<p class="description"><?php esc_html_e( 'Official classification (e.g. 3 for a 3-star hotel, 4 for a 4-star camping).', 'livada-seo' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="schema_number_of_rooms"><?php esc_html_e( 'Number of Rooms / Pitches', 'livada-seo' ); ?></label></th>
					<td>
						<input type="number" min="0" id="schema_number_of_rooms" name="livada_seo[schema_number_of_rooms]" value="<?php echo esc_attr( $s['schema_number_of_rooms'] ?? '' ); ?>" class="small-text" placeholder="80">
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="schema_pets_allowed"><?php esc_html_e( 'Pets Allowed', 'livada-seo' ); ?></label></th>
					<td>
						<select id="schema_pets_allowed" name="livada_seo[schema_pets_allowed]">
							<option value=""<?php selected( $s['schema_pets_allowed'] ?? '', '' ); ?>><?php esc_html_e( '— Unspecified —', 'livada-seo' ); ?></option>
							<option value="1"<?php selected( $s['schema_pets_allowed'] ?? '', '1' ); ?>><?php esc_html_e( 'Yes', 'livada-seo' ); ?></option>
							<option value="0"<?php selected( $s['schema_pets_allowed'] ?? '', '0' ); ?>><?php esc_html_e( 'No', 'livada-seo' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="schema_checkin_time"><?php esc_html_e( 'Check-in Time', 'livada-seo' ); ?></label></th>
					<td>
						<input type="time" id="schema_checkin_time" name="livada_seo[schema_checkin_time]" value="<?php echo esc_attr( $s['schema_checkin_time'] ?? '' ); ?>" class="small-text">
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="schema_checkout_time"><?php esc_html_e( 'Check-out Time', 'livada-seo' ); ?></label></th>
					<td>
						<input type="time" id="schema_checkout_time" name="livada_seo[schema_checkout_time]" value="<?php echo esc_attr( $s['schema_checkout_time'] ?? '' ); ?>" class="small-text">
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="schema_rating_value"><?php esc_html_e( 'Aggregate Rating', 'livada-seo' ); ?></label></th>
					<td>
						<input type="number" min="1" max="5" step="0.1" id="schema_rating_value" name="livada_seo[schema_rating_value]" value="<?php echo esc_attr( $s['schema_rating_value'] ?? '' ); ?>" class="small-text" placeholder="4.7">
						<input type="number" min="1" id="schema_review_count" name="livada_seo[schema_review_count]" value="<?php echo esc_attr( $s['schema_review_count'] ?? '' ); ?>" class="small-text" placeholder="120" style="margin-left:8px;">
						<p class="description">
							<?php esc_html_e( 'Average rating (1–5) and total review count from Google / Tripadvisor / Booking. Both values are required to emit aggregateRating.', 'livada-seo' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>

		<!-- ── Amenities ── -->
		<h2><?php esc_html_e( 'Amenities', 'livada-seo' ); ?></h2>
		<p class="description"><?php esc_html_e( 'Equipments and services available on site (Pool, Wi-Fi, Restaurant, Petanque, Playground, …).', 'livada-seo' ); ?></p>
		<div id="livada-amenities-repeater" class="livada-repeater">
			<?php foreach ( $amenities as $i => $a ) : ?>
				<div class="livada-repeater-row">
					<input type="text" name="livada_seo[schema_amenities][<?php echo (int) $i; ?>][name]" value="<?php echo esc_attr( $a['name'] ?? '' ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Swimming pool', 'livada-seo' ); ?>">
					<label style="margin-left:8px;">
						<input type="checkbox" name="livada_seo[schema_amenities][<?php echo (int) $i; ?>][value]" value="1" <?php checked( ! empty( $a['value'] ) && '0' !== (string) $a['value'], true ); ?>>
						<?php esc_html_e( 'Available', 'livada-seo' ); ?>
					</label>
					<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove amenity', 'livada-seo' ); ?>">&#10005;</button>
				</div>
			<?php endforeach; ?>

			<div class="livada-repeater-row livada-repeater-template" style="display:none;" aria-hidden="true">
				<input type="text" name="livada_seo[schema_amenities][__IDX__][name]" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Wi-Fi', 'livada-seo' ); ?>">
				<label style="margin-left:8px;">
					<input type="checkbox" name="livada_seo[schema_amenities][__IDX__][value]" value="1" checked>
					<?php esc_html_e( 'Available', 'livada-seo' ); ?>
				</label>
				<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove amenity', 'livada-seo' ); ?>">&#10005;</button>
			</div>
		</div>
		<button type="button" class="button livada-repeater-add" data-target="livada-amenities-repeater"><?php esc_html_e( '+ Add Amenity', 'livada-seo' ); ?></button>

		<!-- ── Opening Hours ── -->
		<h2><?php esc_html_e( 'Opening Hours', 'livada-seo' ); ?></h2>
		<div id="livada-opening-hours-repeater" class="livada-repeater">
			<?php foreach ( $opening_hours as $i => $row ) : ?>
				<div class="livada-repeater-row">
					<select name="livada_seo[schema_opening_hours][<?php echo (int) $i; ?>][day]">
						<?php foreach ( $days as $day ) : ?>
							<option value="<?php echo esc_attr( $day ); ?>" <?php selected( $row['day'] ?? '', $day ); ?>><?php echo esc_html( $day ); ?></option>
						<?php endforeach; ?>
					</select>
					<label class="screen-reader-text"><?php esc_html_e( 'Opens', 'livada-seo' ); ?></label>
					<input type="time" name="livada_seo[schema_opening_hours][<?php echo (int) $i; ?>][open]" value="<?php echo esc_attr( $row['open'] ?? '' ); ?>" placeholder="09:00">
					<span><?php esc_html_e( '—', 'livada-seo' ); ?></span>
					<label class="screen-reader-text"><?php esc_html_e( 'Closes', 'livada-seo' ); ?></label>
					<input type="time" name="livada_seo[schema_opening_hours][<?php echo (int) $i; ?>][close]" value="<?php echo esc_attr( $row['close'] ?? '' ); ?>" placeholder="18:00">
					<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove row', 'livada-seo' ); ?>">&#10005;</button>
				</div>
			<?php endforeach; ?>

			<!-- Template row (hidden, cloned by JS) -->
			<div class="livada-repeater-row livada-repeater-template" style="display:none;" aria-hidden="true">
				<select name="livada_seo[schema_opening_hours][__IDX__][day]">
					<?php foreach ( $days as $day ) : ?>
						<option value="<?php echo esc_attr( $day ); ?>"><?php echo esc_html( $day ); ?></option>
					<?php endforeach; ?>
				</select>
				<input type="time" name="livada_seo[schema_opening_hours][__IDX__][open]" placeholder="09:00">
				<span><?php esc_html_e( '—', 'livada-seo' ); ?></span>
				<input type="time" name="livada_seo[schema_opening_hours][__IDX__][close]" placeholder="18:00">
				<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove row', 'livada-seo' ); ?>">&#10005;</button>
			</div>
		</div>
		<button type="button" class="button livada-repeater-add" data-target="livada-opening-hours-repeater"><?php esc_html_e( '+ Add Hours', 'livada-seo' ); ?></button>

		<!-- ── SameAs URLs ── -->
		<h2><?php esc_html_e( 'SameAs URLs', 'livada-seo' ); ?></h2>
		<p class="description"><?php esc_html_e( 'Link to social profiles and other authoritative pages about your business.', 'livada-seo' ); ?></p>
		<div id="livada-sameas-repeater" class="livada-repeater">
			<?php foreach ( $same_as as $i => $url ) : ?>
				<div class="livada-repeater-row">
					<input type="url" name="livada_seo[schema_same_as][<?php echo (int) $i; ?>]" value="<?php echo esc_url( $url ); ?>" class="regular-text" placeholder="https://">
					<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove', 'livada-seo' ); ?>">&#10005;</button>
				</div>
			<?php endforeach; ?>

			<div class="livada-repeater-row livada-repeater-template" style="display:none;" aria-hidden="true">
				<input type="url" name="livada_seo[schema_same_as][__IDX__]" class="regular-text" placeholder="https://">
				<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove', 'livada-seo' ); ?>">&#10005;</button>
			</div>
		</div>
		<button type="button" class="button livada-repeater-add" data-target="livada-sameas-repeater"><?php esc_html_e( '+ Add URL', 'livada-seo' ); ?></button>

		<!-- ── Services ── -->
		<h2><?php esc_html_e( 'Services', 'livada-seo' ); ?></h2>
		<div id="livada-services-repeater" class="livada-repeater livada-repeater--block">
			<?php foreach ( $services as $i => $svc ) : ?>
				<div class="livada-repeater-row livada-repeater-row--block">
					<div class="livada-repeater-row__header">
						<strong><?php esc_html_e( 'Service', 'livada-seo' ); ?> <?php echo (int) $i + 1; ?></strong>
						<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove service', 'livada-seo' ); ?>">&#10005;</button>
					</div>
					<label><?php esc_html_e( 'Name', 'livada-seo' ); ?>
						<input type="text" name="livada_seo[schema_services][<?php echo (int) $i; ?>][name]" value="<?php echo esc_attr( $svc['name'] ?? '' ); ?>" class="regular-text">
					</label>
					<label><?php esc_html_e( 'Description', 'livada-seo' ); ?>
						<textarea name="livada_seo[schema_services][<?php echo (int) $i; ?>][description]" rows="2" class="large-text"><?php echo esc_textarea( $svc['description'] ?? '' ); ?></textarea>
					</label>
				</div>
			<?php endforeach; ?>

			<div class="livada-repeater-row livada-repeater-row--block livada-repeater-template" style="display:none;" aria-hidden="true">
				<div class="livada-repeater-row__header">
					<strong><?php esc_html_e( 'Service', 'livada-seo' ); ?></strong>
					<button type="button" class="button-link livada-repeater-remove" aria-label="<?php esc_attr_e( 'Remove service', 'livada-seo' ); ?>">&#10005;</button>
				</div>
				<label><?php esc_html_e( 'Name', 'livada-seo' ); ?>
					<input type="text" name="livada_seo[schema_services][__IDX__][name]" class="regular-text">
				</label>
				<label><?php esc_html_e( 'Description', 'livada-seo' ); ?>
					<textarea name="livada_seo[schema_services][__IDX__][description]" rows="2" class="large-text"></textarea>
				</label>
			</div>
		</div>
		<button type="button" class="button livada-repeater-add" data-target="livada-services-repeater"><?php esc_html_e( '+ Add Service', 'livada-seo' ); ?></button>

		<br><br>
		<?php submit_button( __( 'Save Settings', 'livada-seo' ) ); ?>
	</form>
</div>
