//! Matrix coverage for the `explain` prefix command (ADR-0028).
//!
//! `explain` wraps `show data` / `update` / `delete` through a
//! `Subgrammar`, so the inner command is parsed, completed and
//! hinted exactly as it is standalone. These cells verify the
//! prefix plugs in cleanly at each typing position and that the
//! complete forms parse as `Command::Explain`.

use crate::typing_surface::*;
use rdbms_playground::input_render::InputState;

#[test]
fn after_explain_offers_the_three_explainable_commands() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["show", "update", "delete"]);
    crate::snap!("after_explain", a);
}

#[test]
fn after_explain_show_expects_data() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain show ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // `explain` covers `show data` only — not `show table`.
    assert_candidate_present(&a, &["data"]);
    assert_no_candidate_named(&a, &["table"]);
    crate::snap!("after_explain_show", a);
}

#[test]
fn after_explain_show_data_offers_table_names() {
    let schema = schema_multi_table();
    let a = assess_at_end("explain show data ", &schema);
    assert_candidate_present(&a, &["Customers", "Orders"]);
    crate::snap!("after_explain_show_data", a);
}

#[test]
fn after_explain_show_data_table_offers_where_and_limit() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain show data Customers ", &schema);
    assert_candidate_present(&a, &["where", "limit"]);
    crate::snap!("after_explain_show_data_table", a);
}

#[test]
fn explain_show_data_where_offers_active_table_columns_no_leakage() {
    let schema = schema_multi_table();
    let a = assess_at_end("explain show data Customers where ", &schema);
    assert_candidate_present(&a, &["id", "Name"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("explain_show_data_where", a);
}

#[test]
fn complete_explain_show_data_parses_as_explain() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain show data Customers", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Explain"));
    crate::snap!("complete_explain_show_data", a);
}

#[test]
fn complete_explain_show_data_with_where_and_limit_parses() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain show data Customers where id = 1 limit 5", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Explain"));
    crate::snap!("complete_explain_show_data_where_limit", a);
}

#[test]
fn after_explain_update_offers_table_names() {
    let schema = schema_multi_table();
    let a = assess_at_end("explain update ", &schema);
    assert_candidate_present(&a, &["Customers", "Orders"]);
    crate::snap!("after_explain_update", a);
}

#[test]
fn after_explain_update_table_expects_set() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain update Customers ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["set"]);
    crate::snap!("after_explain_update_table", a);
}

#[test]
fn complete_explain_update_parses_as_explain() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain update Customers set Name='Bo' where id=1", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Explain"));
    crate::snap!("complete_explain_update", a);
}

#[test]
fn after_explain_delete_expects_from() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain delete ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["from"]);
    crate::snap!("after_explain_delete", a);
}

#[test]
fn after_explain_delete_from_table_expects_filter_clause() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain delete from Customers ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["where", "--all-rows"]);
    crate::snap!("after_explain_delete_from_table", a);
}

#[test]
fn complete_explain_delete_parses_as_explain() {
    let schema = schema_serial_pk();
    let a = assess_at_end("explain delete from Customers where id=1", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Explain"));
    crate::snap!("complete_explain_delete", a);
}
