<?php
namespace Bookaro\Controller\Admin;

defined( 'ABSPATH' ) || exit;

use Bookaro\Service\PrivacyService;

/**
 * Registers privacy policy content and data export/erase tools.
 */
class PrivacyController
{
    private PrivacyService $privacy;

    public function __construct( PrivacyService $privacy )
    {
        $this->privacy = $privacy;
    }

    public function register_hooks(): void
    {
        if ( ! is_admin() ) {
            return;
        }

        add_action( 'admin_init', [ $this, 'register_privacy_tools' ] );
    }

    public function register_privacy_tools(): void
    {
        if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
            wp_add_privacy_policy_content( 'MyBookaro', $this->get_policy_content() );
        }

        if ( function_exists( 'wp_privacy_add_personal_data_exporter' ) ) {
            wp_privacy_add_personal_data_exporter(
                'bookaro-exporter',
                [
                    'exporter_friendly_name' => __( 'MyBookaro Booking Data', 'mybookaro' ),
                    'callback'               => [ $this->privacy, 'export_personal_data' ],
                ]
            );
        } else {
            $this->privacy->register_hooks();
        }

        if ( function_exists( 'wp_privacy_add_personal_data_eraser' ) ) {
            wp_privacy_add_personal_data_eraser(
                'bookaro-eraser',
                [
                    'eraser_friendly_name' => __( 'MyBookaro Booking Data', 'mybookaro' ),
                    'callback'             => [ $this->privacy, 'erase_personal_data' ],
                ]
            );
        }
    }

    private function get_policy_content(): string
    {
        self::register_pro_only_strings();
        $content  = '<p>' . esc_html__( 'MyBookaro Lite stores booking data in your site database to provide scheduling and booking functionality.', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'Data stored may include listing and service IDs, customer and provider user IDs, booking dates, status, price, notes, cancellation reason, and phone number.', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'MyBookaro also stores availability rules, timeslots, date exceptions, and booking settings needed to calculate availability.', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'MyBookaro stores email notification delivery logs (template, status, summary, and errors).', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'MyBookaro does not send data to third parties by default.', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'MyBookaro stores logs for troubleshooting in wp-content/uploads/bookaro/bookaro.log when file logging is enabled. Log file retention can be configured in settings.', 'mybookaro' ) . '</p>';
        $content .= '<p>' . esc_html__( 'In MyBookaro Lite, retention settings apply only to email notification logs and the optional file log.', 'mybookaro' ) . '</p>';
        $retention_text = __( 'Defaults: notification logs are kept for 180 days, and log file retention is disabled unless configured.', 'mybookaro' );
        $content .= '<p>' . esc_html( $retention_text ) . '</p>';
        $content .= '<p>' . esc_html__( 'MyBookaro integrates with the WordPress personal data export and erasure tools. Booking data associated with a user can be exported or anonymized via Tools > Export/Erase Personal Data, or users can use the "Delete My Data" option where available.', 'mybookaro' ) . '</p>';

        $content = apply_filters( 'bookaro_privacy_policy_content', $content );

        return wp_kses_post( $content );
    }

    /**
     * Pro-only strings kept in Lite for translations.
     */
    private static function register_pro_only_strings(): void
    {
        __( 'If chat is enabled, MyBookaro stores chat threads and messages per booking, including participant IDs, message content, and read timestamps.', 'mybookaro' );
        __( 'Chat messages are retained for 730 days by default. You can change chat retention in MyBookaro settings.', 'mybookaro' );
    }

}

