import numpy as np
import pandas as pd

np.random.seed(42)

n_samples = 10000

def temperature_field(x, y, z, t):
    source_1 = np.exp(-((x - 2 * np.sin(t))**2 + (y - 2 * np.cos(t))**2 + (z - np.sin(0.5 * t))**2) / 2)
    source_2 = np.exp(-((x + 2)**2 + (y - 1)**2 + (z + 1)**2) / 3)
    source_3 = np.exp(-((x - 3)**2 + (y + 2)**2 + (z - 2)**2) / 1.5)
    wave = np.sin(2 * np.sqrt(x**2 + y**2 + z**2) - 1.5 * t)
    pattern = np.sin(1.5 * x + t) * np.cos(1.2 * y - 0.5 * t)

    return 20 + 55 * source_1 + 35 * source_2 + 25 * source_3 + 8 * wave + 5 * pattern

def pressure_field(x, y, z, t):
    wave = np.sin(1.5 * x - t) * np.cos(1.2 * y + 0.5 * t)
    source = np.exp(-((x + np.sin(t))**2 + (y - 2)**2 + z**2) / 4)
    pattern = np.sin(x * y + t) * np.cos(z - t)

    return 101325 + 2500 * wave + 1800 * source + 700 * pattern

def velocity_field(x, y, z, t):
    source_1 = np.exp(-((x - 2)**2 + (y + 1)**2 + z**2) / 5)
    source_2 = np.exp(-((x + 2)**2 + (y - 2)**2 + z**2) / 4)

    vx = (
        0.8 * np.sin(y + 0.5 * t)
        + 0.5 * np.cos(x - t)
        + 1.5 * source_1
        - 0.8 * source_2
        + 0.2 * np.sin(x * y)
    )

    vy = (
        0.8 * np.cos(x - 0.4 * t)
        + 0.5 * np.sin(y + t)
        - 1.2 * source_1
        + 1.5 * source_2
        + 0.2 * np.cos(x * y)
    )

    vz = (
        np.sin(x - t) * np.cos(y + z)
        + 0.5 * np.sin(2 * z + t)
    )

    return vx, vy, vz


df_measurements = pd.DataFrame({
    "x": np.random.uniform(-5, 5, n_samples),
    "y": np.random.uniform(-5, 5, n_samples),
    "z": np.random.uniform(-5, 5, n_samples),
    "time": np.random.uniform(0, 10, n_samples)
})

df_measurements["group"] = np.random.choice(
    ["Control", "Experiment A", "Experiment B"],
    n_samples,
    p=[0.33, 0.33, 0.34]
)

group_temperature = df_measurements["group"].map({
    "Control": 0,
    "Experiment A": 5,
    "Experiment B": 10
}).to_numpy()

group_pressure = df_measurements["group"].map({
    "Control": 0,
    "Experiment A": 500,
    "Experiment B": 1000
}).to_numpy()

x = df_measurements["x"].to_numpy()
y = df_measurements["y"].to_numpy()
z = df_measurements["z"].to_numpy()
t = df_measurements["time"].to_numpy()

temperature = temperature_field(x, y, z, t)
pressure = pressure_field(x, y, z, t)
vx, vy, vz = velocity_field(x, y, z, t)

df_measurements["temperature"] = temperature + group_temperature + np.random.normal(0, 2, n_samples)
df_measurements["pressure"] = pressure + group_pressure + np.random.normal(0, 150, n_samples)
df_measurements["velocity_x"] = vx + np.random.normal(0, 0.08, n_samples)
df_measurements["velocity_y"] = vy + np.random.normal(0, 0.08, n_samples)
df_measurements["velocity_z"] = vz + np.random.normal(0, 0.08, n_samples)

df_measurements["energy"] = (
    0.5 * df_measurements["temperature"]
    + 0.0001 * df_measurements["pressure"]
    + 10 * (
        df_measurements["velocity_x"]**2
        + df_measurements["velocity_y"]**2
        + df_measurements["velocity_z"]**2
    )
)

df_measurements["anomaly"] = (
    (df_measurements["temperature"] > 60)
    | (df_measurements["pressure"] > 104000)
    | (df_measurements["energy"] > 65)
)

pd.set_option("display.max_columns", None)

print(df_measurements.head())
print(df_measurements.describe())

print("\nAnomaly distribution:")
print(df_measurements["anomaly"].value_counts())

print("\nExperimental groups:")
print(df_measurements["group"].value_counts())

df_measurements.to_csv("../data/measurements.csv", index=False, float_format="%.2f")