//! Matrix coverage for the complex WHERE-expression surface
//! and `show data` `where` / `limit` (ADR-0026).
//!
//! The simple `where col = val` cells live in
//! `delete_with_where` / `update_with_where`; this file covers
//! the expression grammar's wider surface — operators, the
//! AND / OR connectives, the negatable predicates, and the
//! `show data` filter / limit clauses.

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

#[test]
fn after_where_column_offers_predicate_operators() {
    let schema = schema_serial_pk();
    let a = assess_at_end("delete from Customers where Email ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // After an operand the grammar expects a predicate tail —
    // the negatable keywords surface as candidates.
    assert_candidate_present(&a, &["like", "between", "in", "is"]);
    crate::snap!("after_column", a);
}

#[test]
fn after_complete_predicate_offers_and_or() {
    let schema = schema_serial_pk();
    let a = assess_at_end("delete from Customers where id=1 ", &schema);
    assert!(matches!(a.state, InputState::Valid));
    // A complete predicate can be extended with a connective.
    assert_candidate_present(&a, &["and", "or"]);
    crate::snap!("after_predicate", a);
}

#[test]
fn after_not_keyword_expects_a_predicate() {
    let schema = schema_serial_pk();
    let a = assess_at_end("delete from Customers where not ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // `not` is followed by another predicate — the column
    // operands of the active table are offered.
    assert_candidate_present(&a, &["Email", "Name"]);
    crate::snap!("after_not", a);
}

#[test]
fn between_expects_a_low_bound() {
    let schema = schema_every_type();
    let a = assess_at_end("delete from Things where k between ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    crate::snap!("between_low", a);
}

#[test]
fn in_list_open_paren_expects_an_item() {
    let schema = schema_every_type();
    let a = assess_at_end("delete from Things where k in (", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    crate::snap!("in_open_paren", a);
}

#[test]
fn complex_and_or_expression_parses() {
    let schema = schema_every_type();
    // `note` is the text column — `t` is not a column of
    // `Things`, and the schema-existence diagnostic (ADR-0027)
    // would now correctly flag it; this cell tests a *valid*
    // complex expression, so it uses a real column.
    let a = assess_at_end(
        "delete from Things where k > 1 and note like 'a%' or k = 9",
        &schema,
    );
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Delete"));
    crate::snap!("complex_and_or", a);
}

#[test]
fn show_data_after_table_offers_where_and_limit() {
    let schema = schema_serial_pk();
    let a = assess_at_end("show data Customers ", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_candidate_present(&a, &["where", "limit"]);
    crate::snap!("show_after_table", a);
}

#[test]
fn show_data_after_where_predicate_offers_limit() {
    let schema = schema_serial_pk();
    let a = assess_at_end("show data Customers where id=1 ", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_candidate_present(&a, &["limit", "and", "or"]);
    crate::snap!("show_after_where", a);
}

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