<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

function lt_core_register_data_tools_menu() {
    add_submenu_page( 'lt-core-dashboard', __( 'Data Architecture', 'law-temple-core' ), __( 'Data Architecture', 'law-temple-core' ), 'lt_manage_data_architecture', 'lt-data-architecture', 'lt_core_data_architecture_page' );
}
add_action( 'admin_menu', 'lt_core_register_data_tools_menu', 35 );

function lt_core_data_architecture_page() {
    if ( ! lt_core_current_user_can( 'lt_manage_data_architecture' ) ) { return; }
    if ( isset( $_POST['lt_rebuild_relationship_index'] ) ) {
        check_admin_referer( 'lt_rebuild_relationship_index', 'lt_relationship_index_nonce' );
        lt_core_install_relationship_table();
        $count = lt_core_relationship_index_migrate_all();
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( sprintf( __( 'Relationship index rebuilt from %d content records.', 'law-temple-core' ), $count ) ) . '</p></div>';
    }
    global $wpdb;
    $table = lt_core_relationship_table();
    $exists = lt_core_relationship_table_exists();
    $rows = $exists ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ) : 0; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    $last = get_option( 'lt_core_relationship_index_last_migration', '' );
    echo '<div class="wrap"><h1>' . esc_html__( 'Data Architecture', 'law-temple-core' ) . '</h1>';
    echo '<p>' . esc_html__( 'v3.0.0 keeps the existing post meta for backward compatibility and adds an indexed relationship layer for scalable reverse lookups. Structured professional fields are stored independently from legacy text fields.', 'law-temple-core' ) . '</p>';
    echo '<div class="card" style="max-width:900px;padding:22px"><h2>' . esc_html__( 'Relationship Index', 'law-temple-core' ) . '</h2>';
    echo '<p><strong>' . esc_html__( 'Table:', 'law-temple-core' ) . '</strong> ' . esc_html( $exists ? $table : __( 'Not installed', 'law-temple-core' ) ) . '</p>';
    echo '<p><strong>' . esc_html__( 'Indexed relationships:', 'law-temple-core' ) . '</strong> ' . esc_html( (string) $rows ) . '</p>';
    echo '<p><strong>' . esc_html__( 'Last rebuild:', 'law-temple-core' ) . '</strong> ' . esc_html( $last ?: __( 'Not recorded', 'law-temple-core' ) ) . '</p>';
    echo '<form method="post">'; wp_nonce_field( 'lt_rebuild_relationship_index', 'lt_relationship_index_nonce' );
    submit_button( __( 'Rebuild Relationship Index', 'law-temple-core' ), 'secondary', 'lt_rebuild_relationship_index', false );
    echo '</form><p class="description">' . esc_html__( 'Rebuilding is safe: it reads current relationship meta and recreates only the derived index. It does not delete lawyers, practices, insights or relationship meta.', 'law-temple-core' ) . '</p></div>';
    echo '<div class="card" style="max-width:900px;padding:22px;margin-top:18px"><h2>' . esc_html__( 'Structured Professional Profiles', 'law-temple-core' ) . '</h2><p>' . esc_html__( 'Lawyer edit screens now support repeatable structured records for qualifications, education, Bar admissions, memberships, languages, publications, awards, experience highlights and representative matters. Existing legacy text remains a frontend fallback until structured records are added.', 'law-temple-core' ) . '</p></div>';
    echo '<div class="card" style="max-width:900px;padding:22px;margin-top:18px"><h2>' . esc_html__( 'Structured Practice & Matter Records', 'law-temple-core' ) . '</h2><p>' . esc_html__( 'Practice Areas can store repeatable services and capabilities with individual descriptions. Representative Matters can store repeatable legal-work and outcome records. Existing legacy fields remain compatible and are used whenever structured records have not yet been entered.', 'law-temple-core' ) . '</p></div>';
    echo '<div class="card" style="max-width:900px;padding:22px;margin-top:18px"><h2>' . esc_html__( 'Permissions Architecture', 'law-temple-core' ) . '</h2><p>' . esc_html__( 'v3.0.0 maps each Law Firm content type to dedicated WordPress capabilities and provides purpose-specific firm roles. The permission model is additive and synchronized separately from content, so rebuilding the relationship index never changes user access.', 'law-temple-core' ) . '</p><p><a class="button" href="' . esc_url( admin_url( 'admin.php?page=lt-core-roles' ) ) . '">' . esc_html__( 'Review Roles & Permissions', 'law-temple-core' ) . '</a></p></div>';
    echo '</div>';
}
