using LinqToDB.Data;

namespace Azaion.Flights.Database;

public static class DatabaseMigrator
{
    public static void Migrate(AppDataConnection db)
    {
        db.Execute(Sql);
    }

    private const string Sql = """
        CREATE TABLE IF NOT EXISTS aircrafts (
            id UUID PRIMARY KEY,
            type INTEGER NOT NULL DEFAULT 0,
            model TEXT NOT NULL,
            name TEXT NOT NULL,
            fuel_type INTEGER NOT NULL DEFAULT 0,
            battery_capacity NUMERIC NOT NULL DEFAULT 0,
            engine_consumption NUMERIC NOT NULL DEFAULT 0,
            engine_consumption_idle NUMERIC NOT NULL DEFAULT 0,
            is_default BOOLEAN NOT NULL DEFAULT FALSE
        );

        CREATE TABLE IF NOT EXISTS flights (
            id UUID PRIMARY KEY,
            created_date TIMESTAMP NOT NULL DEFAULT NOW(),
            name TEXT NOT NULL,
            aircraft_id UUID NOT NULL REFERENCES aircrafts(id)
        );

        CREATE TABLE IF NOT EXISTS waypoints (
            id UUID PRIMARY KEY,
            flight_id UUID NOT NULL REFERENCES flights(id),
            lat NUMERIC,
            lon NUMERIC,
            mgrs TEXT,
            waypoint_source INTEGER NOT NULL DEFAULT 0,
            waypoint_objective INTEGER NOT NULL DEFAULT 0,
            order_num INTEGER NOT NULL DEFAULT 0,
            height NUMERIC NOT NULL DEFAULT 0
        );

        CREATE TABLE IF NOT EXISTS orthophotos (
            id TEXT PRIMARY KEY,
            flight_id UUID NOT NULL REFERENCES flights(id),
            name TEXT NOT NULL,
            path TEXT NOT NULL,
            lat NUMERIC,
            lon NUMERIC,
            mgrs TEXT,
            uploaded_at TIMESTAMP NOT NULL DEFAULT NOW()
        );

        CREATE TABLE IF NOT EXISTS gps_corrections (
            id UUID PRIMARY KEY,
            flight_id UUID NOT NULL REFERENCES flights(id),
            waypoint_id UUID NOT NULL REFERENCES waypoints(id),
            original_gps TEXT NOT NULL,
            corrected_gps TEXT NOT NULL,
            applied_at TIMESTAMP NOT NULL DEFAULT NOW()
        );

        CREATE TABLE IF NOT EXISTS map_objects (
            id UUID PRIMARY KEY,
            flight_id UUID NOT NULL REFERENCES flights(id),
            h3_index TEXT NOT NULL,
            mgrs TEXT NOT NULL,
            lat NUMERIC,
            lon NUMERIC,
            class_num INTEGER NOT NULL DEFAULT 0,
            label TEXT NOT NULL DEFAULT '',
            size_width_m NUMERIC NOT NULL DEFAULT 0,
            size_length_m NUMERIC NOT NULL DEFAULT 0,
            confidence NUMERIC NOT NULL DEFAULT 0,
            object_status INTEGER NOT NULL DEFAULT 0,
            first_seen_at TIMESTAMP NOT NULL DEFAULT NOW(),
            last_seen_at TIMESTAMP NOT NULL DEFAULT NOW()
        );

        CREATE INDEX IF NOT EXISTS ix_flights_aircraft_id ON flights(aircraft_id);
        CREATE INDEX IF NOT EXISTS ix_waypoints_flight_id ON waypoints(flight_id);
        CREATE INDEX IF NOT EXISTS ix_orthophotos_flight_id ON orthophotos(flight_id);
        CREATE INDEX IF NOT EXISTS ix_gps_corrections_flight_id ON gps_corrections(flight_id);
        CREATE INDEX IF NOT EXISTS ix_gps_corrections_waypoint_id ON gps_corrections(waypoint_id);
        CREATE INDEX IF NOT EXISTS ix_map_objects_flight_id ON map_objects(flight_id);
        """;
}
