#pragma once
#include <string>
#include <vector>
#include <utility>
#include <type_traits>

namespace easybrain {
enum QueryType { best_move };

struct name { std::string val; explicit name(std::string v) : val(std::move(v)) {} };
using bot_name = name;
using ai_name = name;

struct action { std::string val; explicit action(std::string v) : val(std::move(v)) {} };
using for_action = action;

struct score { double val; explicit score(double v) : val(v) {} };
using give_reward = score;

struct state { std::string val; explicit state(std::string v) : val(std::move(v)) {} };
struct start_with { std::string val; explicit start_with(state s) : val(std::move(s.val)) {} };
struct then_switch_to { std::string val; explicit then_switch_to(state s) : val(std::move(s.val)) {} };

struct when { bool cond; explicit when(bool c) : cond(c) {} };
using if_true = when;

struct speed { float val; explicit speed(float v) : val(v) {} };
using at_speed = speed;

struct what_is { QueryType q; explicit what_is(QueryType t) : q(t) {} };

struct pos2d { float x = 0, y = 0; pos2d(float a = 0, float b = 0) : x(a), y(b) {} };
struct from { float x = 0, y = 0; from(float a, float b) : x(a), y(b) {} from(pos2d p) : x(p.x), y(p.y) {} };
struct to { float x = 0, y = 0; to(float a, float b) : x(a), y(b) {} to(pos2d p) : x(p.x), y(p.y) {} };
using target_pos = to;

struct from_pos {
    pos2d* ref = nullptr;
    explicit from_pos(pos2d& p) : ref(&p) {}
};

inline std::pair<std::string, int> chance(std::string opt, int weight) { return {std::move(opt), weight}; }

namespace detail {
    template<class T> T ex(T d) { return d; }
    template<class T, class F, class... R> T ex(T d, F&& f, R&&... r) {
        if constexpr (std::is_same_v<T, std::decay_t<F>>) return f;
        else if constexpr (sizeof...(R) > 0) return ex<T>(d, std::forward<R>(r)...);
        else return d;
    }
}

void reward_impl(name, action, score);
std::string best_action_impl(name);
std::string choose_impl(std::vector<std::pair<std::string, int>> options);
void make_bot_impl(name, state);
void set_state_impl(name, state, when);
std::string get_state_impl(name);
pos2d step_to_impl(from, to, speed);

// Новый English-like API
template<class... A> void teach_ai(A&&... a) { reward_impl(detail::ex(name("ai"), a...), detail::ex(action("idle"), a...), detail::ex(score(1.0), a...)); }
template<class... A> std::string ask_ai(A&&... a) { return best_action_impl(detail::ex(name("ai"), a...)); }
inline std::string decide_between(std::vector<std::pair<std::string, int>> opts) { return choose_impl(std::move(opts)); }
template<class... A> void make_bot(A&&... a) {
    auto sw = detail::ex(start_with(state("idle")), a...);
    auto st = detail::ex(state(sw.val), a...);
    make_bot_impl(detail::ex(name("bot"), a...), st);
}
template<class... A> void bot_rule(A&&... a) {
    auto sw = detail::ex(then_switch_to(state("idle")), a...);
    auto st = detail::ex(state(sw.val), a...);
    set_state_impl(detail::ex(name("bot"), a...), st, detail::ex(when(true), a...));
}
template<class... A> std::string get_state(A&&... a) { return get_state_impl(detail::ex(name("bot"), a...)); }
template<class... A> void bot_chase(A&&... a) {
    auto fp = detail::ex(from_pos(*(pos2d*)nullptr), a...);
    if (!fp.ref) return;
    *fp.ref = step_to_impl(from(*fp.ref), detail::ex(to(0, 0), a...), detail::ex(speed(1.0f), a...));
}

// Обратная совместимость
template<class... A> void reward(A&&... a) { teach_ai(std::forward<A>(a)...); }
template<class... A> std::string best_action(A&&... a) { return ask_ai(std::forward<A>(a)...); }
inline std::string choose(std::vector<std::pair<std::string, int>> opts) { return choose_impl(std::move(opts)); }
template<class... A> void set_state(A&&... a) { bot_rule(std::forward<A>(a)...); }
template<class... A> pos2d step_to(A&&... a) { return step_to_impl(detail::ex(from(0, 0), a...), detail::ex(to(0, 0), a...), detail::ex(speed(1.0f), a...)); }
}
using namespace easybrain;
