=== Smart Price Sync for Homey & PriceLabs ===
Contributors: thor486
Tags: pricing, sync, real estate, booking, dynamic pricing
Requires at least: 5.0
Tested up to: 6.9
Stable tag: 1.1.0
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Synchronizes PriceLabs dynamic pricing and availability data with the Homey WordPress theme listings.

== Description ==

**Smart Price Sync** bridges the gap between the **Homey** WordPress theme and the **PriceLabs** dynamic pricing engine.

Automatically update your listing prices and availability calendars without manual intervention.

### Features
*   **Base Price Sync:** Updates the nightly price in Homey based on PriceLabs data.
*   **Calendar Sync:** Updates daily rates for the next 365 days.
*   **Automated Scheduling:** Choose from Hourly, Twice Daily, or Daily sync intervals.
*   **Manual Sync:** Trigger an immediate update directly from the listing edit page.
*   **Freemium Model:** 
    *   **Free:** Link and sync **1 listing**.
    *   **PRO:** Upgrade to sync **unlimited listings**.

### How it Works
1.  Enter your PriceLabs API Key in the settings.
2.  Add the PriceLabs Listing ID to your Homey Listing.
3.  Select the listing in the "Active Listings" settings to enable sync.
4.  The plugin handles the rest, fetching data in the background via WP Cron.

== Installation ==

1.  Upload the plugin files to the `/wp-content/plugins/smart-price-sync` directory, or install the plugin through the WordPress plugins screen.
2.  Activate the plugin through the 'Plugins' screen in WordPress.
3.  Navigate to **Smart Price Sync** in the admin menu to configure your API Key.

== Frequently Asked Questions ==

= Where do I find my PriceLabs Listing ID? =
Open your PriceLabs calendar for a specific listing. The ID is often found in the URL (e.g., `.../calendar/12345___67890`) or displayed in the dashboard details.

= Does this work with other themes? =
No. This plugin is specifically built for the **Homey** theme and relies on its specific data structure (meta keys).

= How often does it sync? =
You can configure the sync interval in the settings page (Hourly, Twice Daily, or Daily), or leave it on Manual Only and sync from the listing edit screen.

Scheduled syncs run through WP Cron, which is triggered by site traffic. If your site has `DISABLE_WP_CRON` set, make sure `wp-cron.php` is called by a real system cron job, otherwise the scheduled sync will not fire.

= Sync fails with "Sync prices toggle is OFF". What does that mean? =
PriceLabs only serves the day-by-day price calendar through its Customer API for listings that are actively syncing. If a listing's "Sync prices" toggle is OFF in PriceLabs, the API answers with `LISTING_TOGGLE_OFF` and returns no rates, no matter how the plugin is configured. Either turn the toggle ON in PriceLabs, or enable **Base Price Only** in the plugin settings to import the listing base price instead of the daily calendar.

= Sync fails with "Please add a payment method under Billing section". =
That message comes from PriceLabs, not from the plugin. The PriceLabs Customer API is a paid add-on (currently $1 per syncing listing per month) and stays closed until a payment method is on file, even when your API key is valid. Add one under Billing in your PriceLabs account and the calls start working.

= Which PMS does the plugin send to PriceLabs? =
The one PriceLabs reports for that listing. The plugin reads it automatically from your account, so the "Default PMS" field is only a fallback for listings where PriceLabs does not return a PMS.

== Screenshots ==
1.  **General Settings:** API Key input and Sync Interval configuration.
2.  **Activation:** Select which listings to sync (Free vs Pro).
3.  **Listing Setup:** Enter the PriceLabs ID directly in the listing editor.

== Changelog ==

= 1.1.0 =
*   Fixed: the PMS sent to PriceLabs was hardcoded to `lodgify`. It is now read from your PriceLabs account for each listing, with an optional manual fallback.
*   Fixed: the listings response was parsed at the wrong level, so the lookup that resolves each listing's PMS never matched and the hardcoded value was always used. Both the current `{"listings": [...]}` shape and the older bare array are now handled.
*   Added: `LISTING_SUBSCRIPTION_INACTIVE` is reported as an explicit message when the PriceLabs subscription is not active.
*   Fixed: the scheduled sync was never registered with WP Cron, so the "Sync Interval" setting had no effect and only manual syncs ran. The schedule is now created on activation, updated when the interval is changed, and removed on deactivation. Existing installs pick it up on the next admin page load.
*   Added: **Base Price Only** mode, which imports the listing base price for listings whose "Sync prices" toggle is OFF in PriceLabs.
*   Improved: PriceLabs error states (`LISTING_TOGGLE_OFF`, `LISTING_NOT_PRESENT`, `LISTING_NO_DATA`) are now reported as readable messages instead of a raw API dump.
*   Improved: authenticates with the documented `X-API-Key` header, and reports HTTP and API-key errors explicitly.
*   Improved: settings are sanitized on save.

= 1.0.0 =
*   Initial release.
*   Implemented remote license validation for PRO upgrade.
*   Added automatic and manual sync capabilities.
