#include "fesa/analysis/analysis_model.hpp"

#include <gtest/gtest.h>

#include <filesystem>
#include <string>
#include <utility>
#include <vector>

namespace {

fesa::ModelDefinition makeDefinition() {
    const std::filesystem::path source{"models/analysis-model.inp"};
    fesa::ModelDefinition definition{};
    definition.sourcePath = source;
    definition.sourceContentIdentity = "fnv1a64:0123456789abcdef";
    definition.nodes = {
        {{"Beam-1", 1, "1"}, {0.0, 0.0, 0.0}, {source, 10U}},
        {{"Beam-1", 2, "2"}, {1.0, 0.0, 0.0}, {source, 11U}},
        {{"Beam-1", 3, "3"}, {2.0, 0.0, 0.0}, {source, 12U}},
        {{"Beam-1", 4, "4"}, {3.0, 0.0, 0.0}, {source, 13U}},
        {{"Beam-1", 5, "5"}, {4.0, 0.0, 0.0}, {source, 14U}}};
    definition.materials = {
        {"Material-0", 100.0, 0.20, {source, 20U}},
        {"Material-1", 200.0, 0.25, {source, 21U}},
        {"Material-2", 300.0, 0.30, {source, 22U}},
        {"Unused-Material", 400.0, 0.35, {source, 23U}}};
    definition.sections = {
        {"Section-0", 1.0, 1.0, 0.0, 1.0, 1.0,
         {0.0, 1.0, 0.0}, {}, {source, 30U}},
        {"Section-1", 2.0, 2.0, 0.0, 2.0, 2.0,
         {0.0, 1.0, 0.0}, {}, {source, 31U}},
        {"Section-2", 3.0, 3.0, 0.0, 3.0, 3.0,
         {0.0, 1.0, 0.0}, {}, {source, 32U}},
        {"Unused-Section", 4.0, 4.0, 0.0, 4.0, 4.0,
         {0.0, 1.0, 0.0}, {}, {source, 33U}}};
    definition.elements = {
        {{"Beam-1", 1, "1"}, {0U, 1U}, 2U, 2U, {source, 40U}},
        {{"Beam-1", 2, "2"}, {1U, 2U}, 0U, 1U, {source, 41U}},
        {{"Beam-1", 3, "3"}, {2U, 3U}, 2U, 2U, {source, 42U}},
        {{"Beam-1", 4, "4"}, {3U, 4U}, 1U, 0U, {source, 43U}}};
    definition.steps = {{
        "Step-1",
        {{"Root", 1, 3, 0.0, {source, 51U}},
         {"Root", 4, 6, 0.0, {source, 52U}}},
        {{"Tip", 1, 10.0, {source, 53U}},
         {"Tip", 2, -20.0, {source, 54U}},
         {"Tip", 6, 30.0, {source, 55U}}},
        0.1,
        1.0,
        0.01,
        1.0,
        {source, 50U}}};
    return definition;
}

} // namespace

TEST(AnalysisModel, ClassifiesActiveEntitiesInStableOrder) {
    auto domainResult = fesa::Domain::create(makeDefinition());
    ASSERT_TRUE(domainResult.hasValue());

    auto modelResult = fesa::AnalysisModel::create(domainResult.value());
    ASSERT_TRUE(modelResult.hasValue());
    const auto& model = modelResult.value();

    EXPECT_EQ(
        model.activeElements(),
        (std::vector<fesa::EntityIndex>{0U, 1U, 2U, 3U}));
    EXPECT_EQ(
        model.activeMaterials(),
        (std::vector<fesa::EntityIndex>{0U, 1U, 2U}));
    EXPECT_EQ(
        model.activeSections(),
        (std::vector<fesa::EntityIndex>{0U, 1U, 2U}));
    EXPECT_EQ(
        model.activeBoundaryConditions(),
        (std::vector<fesa::EntityIndex>{0U, 1U}));
    EXPECT_EQ(
        model.activeLoads(),
        (std::vector<fesa::EntityIndex>{0U, 1U, 2U}));
}

TEST(AnalysisModel, ReferencesWithoutCopyingOrMutatingDomain) {
    auto domainResult = fesa::Domain::create(makeDefinition());
    ASSERT_TRUE(domainResult.hasValue());
    const fesa::Domain& domain = domainResult.value();
    const auto* const elementAddress = domain.elements().data();
    const auto* const materialAddress = domain.materials().data();
    const auto* const sectionAddress = domain.sections().data();
    const std::string stepName = domain.steps()[0].name;
    const double firstLoadMagnitude = domain.steps()[0].loads[0].magnitude;

    auto modelResult = fesa::AnalysisModel::create(domain);
    ASSERT_TRUE(modelResult.hasValue());
    const auto& model = modelResult.value();

    EXPECT_EQ(&model.domain(), &domain);
    EXPECT_EQ(&model.step(), &domain.steps()[0]);
    EXPECT_EQ(model.domain().elements().data(), elementAddress);
    EXPECT_EQ(model.domain().materials().data(), materialAddress);
    EXPECT_EQ(model.domain().sections().data(), sectionAddress);
    EXPECT_EQ(
        &model.domain().elements()[model.activeElements()[1]],
        &domain.elements()[1]);
    EXPECT_EQ(
        &model.domain().materials()[model.activeMaterials()[2]],
        &domain.materials()[2]);
    EXPECT_EQ(
        &model.domain().sections()[model.activeSections()[1]],
        &domain.sections()[1]);
    EXPECT_EQ(domain.steps()[0].name, stepName);
    EXPECT_DOUBLE_EQ(domain.steps()[0].loads[0].magnitude, firstLoadMagnitude);
}

TEST(AnalysisModel, RejectsMissingOrMultipleStep) {
    auto missingDefinition = makeDefinition();
    missingDefinition.steps.clear();
    auto missingDomain = fesa::Domain::create(std::move(missingDefinition));
    ASSERT_TRUE(missingDomain.hasValue());

    auto missing = fesa::AnalysisModel::create(missingDomain.value());
    ASSERT_FALSE(missing.hasValue());
    EXPECT_EQ(
        missing.status().failureCategory(),
        fesa::FailureCategory::input);
    ASSERT_EQ(missing.status().diagnostics().size(), 1U);
    EXPECT_EQ(
        missing.status().diagnostics()[0].code,
        "invalid-model-cardinality");
    EXPECT_EQ(missing.status().diagnostics()[0].keyword, "STEP");
    EXPECT_EQ(missing.status().diagnostics()[0].entityIdentity, "0");

    auto multipleDefinition = makeDefinition();
    auto secondStep = multipleDefinition.steps.front();
    secondStep.name = "Step-2";
    secondStep.location.line = 60U;
    multipleDefinition.steps.push_back(std::move(secondStep));
    auto multipleDomain = fesa::Domain::create(std::move(multipleDefinition));
    ASSERT_TRUE(multipleDomain.hasValue());

    auto multiple = fesa::AnalysisModel::create(multipleDomain.value());
    ASSERT_FALSE(multiple.hasValue());
    EXPECT_EQ(
        multiple.status().failureCategory(),
        fesa::FailureCategory::input);
    ASSERT_EQ(multiple.status().diagnostics().size(), 1U);
    EXPECT_EQ(
        multiple.status().diagnostics()[0].code,
        "unsupported-multiple-step");
    EXPECT_EQ(multiple.status().diagnostics()[0].keyword, "STEP");
    EXPECT_EQ(multiple.status().diagnostics()[0].entityIdentity, "Step-2");
    EXPECT_EQ(multiple.status().diagnostics()[0].location.line, 60U);
}
