//! Matrix coverage for `add 1:n relationship [as <name>]
//! from <P>.<c> to <C>.<c> [on delete <a>] [on update <a>]
//! [--create-fk]` (ADR-0013).

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

#[test]
fn after_add_offers_relationship_branch() {
    let schema = schema_multi_table();
    let a = assess_at_end("add ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // `add` branches to `column` or the `1:n` composite.
    assert_candidate_present(&a, &["column"]);
    crate::snap!("after_add", a);
}

#[test]
fn one_n_relationship_keyword_sequence_is_incomplete() {
    let schema = schema_multi_table();
    let a = assess_at_end("add 1:n relationship ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    // `as` (optional name) or `from` (endpoints) come next.
    assert_candidate_present(&a, &["from"]);
    crate::snap!("after_relationship_keyword", a);
}

#[test]
fn after_from_offers_table_names() {
    let schema = schema_multi_table();
    let a = assess_at_end("add 1:n relationship from ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["Customers", "Orders"]);
    crate::snap!("after_from", a);
}

#[test]
fn after_parent_table_dot_narrows_to_parent_columns() {
    // §2.2 follow-up — `from Customers.` narrows to Customers.
    let schema = schema_multi_table();
    let a = assess_at_end("add 1:n relationship from Customers.", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["id", "Name"]);
    assert_no_candidate_named(&a, &["OrderId", "CustId", "Total"]);
    crate::snap!("after_parent_dot", a);
}

#[test]
fn after_child_table_dot_narrows_to_child_columns() {
    let schema = schema_multi_table();
    let a = assess_at_end("add 1:n relationship from Customers.id to Orders.", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    assert_candidate_present(&a, &["OrderId", "CustId", "Total"]);
    assert_no_candidate_named(&a, &["Name"]);
    crate::snap!("after_child_dot", a);
}

#[test]
fn complete_add_relationship_parses() {
    let schema = schema_multi_table();
    let a = assess_at_end(
        "add 1:n relationship from Customers.id to Orders.CustId",
        &schema,
    );
    assert!(matches!(a.state, InputState::Valid));
    assert_eq!(a.parse_result.as_deref(), Ok("AddRelationship"));
    crate::snap!("complete", a);
}

#[test]
fn add_relationship_with_create_fk_flag_parses() {
    let schema = schema_multi_table();
    let a = assess_at_end(
        "add 1:n relationship from Customers.id to Orders.CustId --create-fk",
        &schema,
    );
    assert!(matches!(a.state, InputState::Valid));
    crate::snap!("with_create_fk", a);
}

#[test]
fn add_relationship_with_on_delete_clause_parses() {
    let schema = schema_multi_table();
    let a = assess_at_end(
        "add 1:n relationship from Customers.id to Orders.CustId on delete cascade",
        &schema,
    );
    assert!(matches!(a.state, InputState::Valid));
    crate::snap!("with_on_delete", a);
}

#[test]
fn add_relationship_in_progress_after_dot_is_incomplete() {
    let schema = schema_multi_table();
    let a = assess_at_end("add 1:n relationship from Customers.id to ", &schema);
    assert!(matches!(a.state, InputState::IncompleteAtEof));
    crate::snap!("in_progress_after_to", a);
}
