//! Matrix coverage for `update T set col=val where col=val`.
//!
//! Per ADR-0014, update requires either WHERE or `--all-rows`.
//! Handoff §1 bug E1 (`update T set ` showing every table's
//! columns) was fixed in commit 619a8bd via
//! `completion_probe`; these tests pin the per-table narrowing
//! invariant.

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

#[test]
fn after_update_keyword_expects_table_name() {
    let schema = schema_multi_table();
    let a = assess_at_end("update ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["Customers", "Orders"]);
    crate::snap!("after_update", a);
}

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

// =========================================================
// Handoff §1 bug E1 — `update T set ` must narrow column
// candidates to the active table.
// =========================================================

#[test]
fn after_set_offers_only_active_table_columns_no_leakage() {
    let schema = schema_multi_table();
    let a = assess_at_end("update Customers set ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // Customers's columns must be offered.
    assert_candidate_present(&a, &["id", "Name"]);
    // Orders's columns must NOT leak.
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("after_set_multi_table", a);
}

#[test]
fn after_set_serial_pk_offers_all_columns() {
    let schema = schema_serial_pk();
    let a = assess_at_end("update Customers set ", &schema);
    assert_candidate_present(&a, &["id", "Name", "Email"]);
    crate::snap!("after_set_serial_pk", a);
}

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

#[test]
fn after_equals_offers_typed_slot_prose_for_column() {
    let schema = schema_serial_pk();
    let a = assess_at_end("update Customers set Email=", &schema);
    let prose = hint_prose(&a).unwrap_or_else(|| panic!("expected Prose, got {:?}", a.hint));
    assert!(
        prose.contains("Email"),
        "should name `Email`, got prose: {prose:?}",
    );
    assert!(
        prose.contains("quoted string"),
        "should say `quoted string` (text type), got prose: {prose:?}",
    );
    crate::snap!("after_equals_text_col", a);
}

#[test]
fn after_equals_for_date_column_says_yyyy_mm_dd() {
    let schema = schema_every_type();
    let a = assess_at_end("update Things set dt=", &schema);
    let prose = hint_prose(&a).unwrap_or_else(|| panic!("expected Prose, got {:?}", a.hint));
    assert!(
        prose.contains("YYYY-MM-DD"),
        "date-slot prose should reference YYYY-MM-DD, got {prose:?}",
    );
    crate::snap!("after_equals_date_col", a);
}

#[test]
fn mid_assignment_list_after_comma_offers_remaining_columns() {
    let schema = schema_multi_table();
    let a = assess_at_end("update Customers set Name='x', ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // Customers columns offered, no leakage.
    assert_candidate_present(&a, &["id"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("mid_assignment_list", a);
}

#[test]
fn after_assignments_expects_where_or_all_rows() {
    let schema = schema_serial_pk();
    let a = assess_at_end("update Customers set Email='new@b.c' ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["where", "--all-rows"]);
    crate::snap!("after_assignments", a);
}

// =========================================================
// WHERE clause column position — also schema-narrowed.
// =========================================================

#[test]
fn after_where_keyword_offers_active_table_columns() {
    let schema = schema_multi_table();
    let a = assess_at_end("update Customers set Name='x' where ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["id", "Name"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("after_where", a);
}

#[test]
fn after_where_column_equals_offers_typed_prose() {
    let schema = schema_serial_pk();
    let a = assess_at_end("update Customers set Email='x' where id=", &schema);
    let prose = hint_prose(&a).unwrap_or_else(|| panic!("expected Prose, got {:?}", a.hint));
    assert!(
        prose.contains("id"),
        "should name where column `id`, got prose: {prose:?}",
    );
    crate::snap!("after_where_equals", a);
}

// =========================================================
// Complete update parses to Update.
// =========================================================

#[test]
fn complete_update_with_where_parses() {
    let schema = schema_serial_pk();
    let a = assess_at_end("update Customers set Email='new@b.c' where id=1", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("Update"));
    crate::snap!("complete_update", a);
}
