//! Matrix coverage for `rename column [in] [table] <T> : <col>
//! to <new>` and `change column [in] [table] <T> : <col>
//! ( <type> ) [--force-conversion | --dont-convert]` (ADR-0013,
//! ADR-0017).
//!
//! Both share the DDL `TABLE_NAME_EXISTING` slot, which now
//! sets `writes_table` (handoff-13 §2.2) — so the column-name
//! slot after the table narrows to that table's columns.

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

// ---- rename column ----

#[test]
fn rename_after_colon_narrows_to_active_table_columns() {
    let schema = schema_multi_table();
    let a = assess_at_end("rename column in Customers: ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["id", "Name"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("rename_after_colon_multi_table", a);
}

#[test]
fn rename_after_old_column_expects_to() {
    let schema = schema_serial_pk();
    let a = assess_at_end("rename column in Customers: Email ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["to"]);
    crate::snap!("rename_after_old_column", a);
}

#[test]
fn rename_after_to_is_new_name_slot() {
    let schema = schema_serial_pk();
    let a = assess_at_end("rename column in Customers: Email to ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    crate::snap!("rename_after_to", a);
}

#[test]
fn complete_rename_column_parses() {
    let schema = schema_serial_pk();
    let a = assess_at_end("rename column in Customers: Email to ContactEmail", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("RenameColumn"));
    crate::snap!("complete_rename", a);
}

// ---- change column ----

#[test]
fn change_after_colon_narrows_to_active_table_columns() {
    let schema = schema_multi_table();
    let a = assess_at_end("change column in Customers: ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["id", "Name"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("change_after_colon_multi_table", a);
}

#[test]
fn change_after_open_paren_offers_type_candidates() {
    let schema = schema_serial_pk();
    let a = assess_at_end("change column in Customers: Email (", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["text", "int", "bool", "decimal"]);
    crate::snap!("change_after_open_paren", a);
}

#[test]
fn complete_change_column_parses() {
    let schema = schema_serial_pk();
    let a = assess_at_end("change column in Customers: Email (text)", &schema);
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("ChangeColumnType"));
    crate::snap!("complete_change", a);
}

#[test]
fn change_column_with_force_conversion_flag_parses() {
    let schema = schema_serial_pk();
    let a = assess_at_end(
        "change column in Customers: Email (int) --force-conversion",
        &schema,
    );
    assert!(matches!(a.state, InputState::Valid));
    crate::snap!("change_with_force", a);
}
