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54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
"""
DQA-OpenEnv Environment — main environment class.

This module implements the core Environment interface for the Data Quality
Assurance OpenEnv competition. It wires together DatasetFactory, RewardEngine,
DQAGrader, and the Pydantic models to provide a complete reset/step loop.
"""

import pandas as pd
import numpy as np
from uuid import uuid4
from typing import Optional, Dict, Any, List, Tuple
from copy import deepcopy

from openenv.core.env_server.interfaces import Environment
from openenv.core.env_server.types import State

import sys
import os

try:
    from ..models import DQAAction, DQAObservation, DQAState
except ImportError:
    try:
        from models import DQAAction, DQAObservation, DQAState
    except ImportError:
        sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        from models import DQAAction, DQAObservation, DQAState

try:
    from .datasets.generators import DatasetFactory
    from .rewards.reward_engine import RewardEngine, QualityScorer
    from .graders.grader import DQAGrader
except ImportError:
    from server.datasets.generators import DatasetFactory
    from server.rewards.reward_engine import RewardEngine, QualityScorer
    from server.graders.grader import DQAGrader


class DqaOpenenvEnvironment(Environment):
    """
    Core OpenEnv environment for Data Quality Assurance.

    Manages the full episode lifecycle: reset → step → done,
    applying agent actions to a dirty DataFrame and scoring
    quality improvement against a hidden ground truth.
    """

    SUPPORTS_CONCURRENT_SESSIONS: bool = True

    def __init__(self) -> None:
        """Initializes the environment with default empty state."""
        self._episode_id: str = str(uuid4())
        self._step_count: int = 0
        self._task_id: str = "easy"
        self._dirty_df: Optional[pd.DataFrame] = None
        self._clean_df: Optional[pd.DataFrame] = None
        self._agent_df: Optional[pd.DataFrame] = None
        self._reward_engine: Optional[RewardEngine] = None
        self._grader: DQAGrader = DQAGrader()
        self._current_scores: Dict[str, float] = {}
        self._action_history: List[str] = []
        self._episode_reward: float = 0.0
        self._is_submitted: bool = False
        self._max_steps: int = 20
        self._task_config: Dict[str, Any] = {}

    def reset(self, task_id: str = "easy") -> DQAObservation:
        """
        Resets the environment for a new episode.

        Args:
            task_id: The difficulty tier — "easy", "medium", or "hard".

        Returns:
            The initial DQAObservation for the new episode.
        """
        self._task_id = task_id
        self._task_config = DatasetFactory.get_task_config(task_id)
        self._max_steps = self._task_config["max_steps"]

        self._dirty_df, self._clean_df = DatasetFactory.get_task_data(task_id)
        self._agent_df = deepcopy(self._dirty_df)

        self._episode_id = str(uuid4())
        self._step_count = 0
        self._action_history = []
        self._episode_reward = 0.0
        self._is_submitted = False
        self._reward_engine = RewardEngine(task_id)

        self._current_scores = QualityScorer.score(
            self._agent_df, self._clean_df, task_id
        )
        # Strictly open interval enforcement — validator rejects 0.0 and 1.0
        self._current_scores = {
            k: max(1e-4, min(1.0 - 1e-4, float(v)))
            for k, v in self._current_scores.items()
        }

        return self._build_observation(
            last_action_result="Environment reset. Task started.",
            last_action_success=True
        )

    def step(self, action: DQAAction) -> DQAObservation:
        """
        Executes a single agent action and returns the resulting observation.

        Args:
            action: The DQAAction to execute.

        Returns:
            A DQAObservation reflecting the post-action state.

        Raises:
            RuntimeError: If reset() has not been called yet.
        """
        if self._agent_df is None:
            raise RuntimeError("Call reset() before step()")

        if self._is_submitted or self._step_count >= self._max_steps:
            return self._build_observation(
                last_action_result="Episode already finished.",
                last_action_success=False,
                done=True
            )

        self._step_count += 1

        action_success, action_result_msg = self._apply_action(action)

        if action.action_type == "submit":
            self._is_submitted = True

        step_reward, new_scores = self._reward_engine.calculate_step_reward(
            action_type=action.action_type,
            action_success=action_success,
            agent_df=self._agent_df,
            clean_df=self._clean_df,
            previous_scores=self._current_scores,
            action_history=self._action_history
        )
        self._current_scores = new_scores
        # Strictly open interval enforcement — validator rejects 0.0 and 1.0
        self._current_scores = {
            k: max(1e-4, min(1.0 - 1e-4, float(v)))
            for k, v in self._current_scores.items()
        }
        self._episode_reward += step_reward

        self._action_history.append(action.action_type)

        done = self._is_submitted or (self._step_count >= self._max_steps)

        return self._build_observation(
            reward=step_reward,
            last_action_result=action_result_msg,
            last_action_success=action_success,
            done=done
        )

    def _apply_action(self, action: DQAAction) -> Tuple[bool, str]:
        """
        Applies the given action to self._agent_df in place.

        Args:
            action: The DQAAction to apply.

        Returns:
            A tuple of (success: bool, message: str).
        """
        try:
            action_type = action.action_type
            col = action.column
            params = action.parameters or {}

            if action_type == "noop":
                return (True, "No operation performed.")

            if action_type == "submit":
                return (True, "Agent submitted the dataset for final grading.")

            if action_type == "fill_nulls":
                strategy = params.get("strategy", "mean")
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                if strategy == "mean":
                    val = self._agent_df[col].mean()
                    self._agent_df[col] = self._agent_df[col].fillna(val)
                elif strategy == "median":
                    val = self._agent_df[col].median()
                    self._agent_df[col] = self._agent_df[col].fillna(val)
                elif strategy == "mode":
                    val = self._agent_df[col].mode()[0]
                    self._agent_df[col] = self._agent_df[col].fillna(val)
                elif strategy == "constant":
                    val = params.get("value", 0)
                    self._agent_df[col] = self._agent_df[col].fillna(val)
                elif strategy == "drop":
                    self._agent_df.dropna(subset=[col], inplace=True)
                    self._agent_df.reset_index(drop=True, inplace=True)
                else:
                    return (False, f"Unknown fill strategy: {strategy}")
                return (True, f"Filled nulls in '{col}' using strategy '{strategy}'.")

            if action_type == "drop_duplicates":
                subset = params.get("subset", None)
                before = len(self._agent_df)
                self._agent_df.drop_duplicates(subset=subset, inplace=True)
                self._agent_df.reset_index(drop=True, inplace=True)
                after = len(self._agent_df)
                return (True, f"Removed {before - after} duplicate rows.")

            if action_type == "cast_type":
                target_type = params.get("target_type", "str")
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                type_map = {"int": "Int64", "float": "float64",
                            "str": "str", "bool": "bool"}
                dtype = type_map.get(target_type, target_type)
                # Round numeric columns before int casting to handle float medians
                if target_type in ("int",) and pd.api.types.is_float_dtype(self._agent_df[col]):
                    self._agent_df[col] = self._agent_df[col].round(0)
                self._agent_df[col] = self._agent_df[col].astype(dtype)
                return (True, f"Cast column '{col}' to type '{target_type}'.")

            if action_type == "normalize_category":
                mapping = params.get("mapping", {})
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                self._agent_df[col] = self._agent_df[col].replace(mapping)
                return (True, f"Normalized {len(mapping)} category values in '{col}'.")

            if action_type == "clip_outliers":
                method = params.get("method", "iqr")
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                if method == "iqr":
                    Q1 = self._agent_df[col].quantile(0.25)
                    Q3 = self._agent_df[col].quantile(0.75)
                    IQR = Q3 - Q1
                    lower = Q1 - 1.5 * IQR
                    upper = Q3 + 1.5 * IQR
                    self._agent_df[col] = self._agent_df[col].clip(lower, upper)
                elif method == "zscore":
                    mean = self._agent_df[col].mean()
                    std = self._agent_df[col].std()
                    lower = mean - 3 * std
                    upper = mean + 3 * std
                    self._agent_df[col] = self._agent_df[col].clip(lower, upper)
                else:
                    return (False, f"Unknown clip method: {method}")
                return (True, f"Clipped outliers in '{col}' using '{method}' method.")

            if action_type == "drop_column":
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                self._agent_df.drop(columns=[col], inplace=True)
                return (True, f"Dropped column '{col}'.")

            if action_type == "filter_rows":
                condition = params.get("condition", "")
                if not condition:
                    return (False, "No condition provided for filter_rows.")
                blocked = ["__", "import", "exec", "eval", "open", "@", "lambda"]
                if any(b in condition for b in blocked):
                    return (False, f"Unsafe filter condition rejected: {condition}")
                before = len(self._agent_df)
                self._agent_df.query(condition, inplace=True)
                self._agent_df.reset_index(drop=True, inplace=True)
                after = len(self._agent_df)
                return (True, f"Filtered {before - after} rows matching '{condition}'.")

            if action_type == "merge_categories":
                from_values = params.get("from_values", [])
                to_value = params.get("to_value", "")
                if col not in self._agent_df.columns:
                    return (False, f"Column '{col}' not found.")
                mapping = {v: to_value for v in from_values}
                self._agent_df[col] = self._agent_df[col].replace(mapping)
                return (True, f"Merged {len(from_values)} categories into '{to_value}' in '{col}'.")

            return (False, f"Unknown action_type: {action_type}")

        except Exception as e:
            return (False, f"Action failed with error: {str(e)}")

    def _build_observation(
        self,
        reward: float = 0.0,
        last_action_result: str = "",
        last_action_success: bool = True,
        done: bool = False
    ) -> DQAObservation:
        """
        Constructs a DQAObservation from the current environment state.

        Args:
            reward:              The step reward to include.
            last_action_result:  Human-readable result message.
            last_action_success: Whether the last action succeeded.
            done:                Whether the episode is over.

        Returns:
            A fully populated DQAObservation.
        """
        # Dataset preview
        if self._agent_df is not None:
            preview_df = self._agent_df.head(10)
            dataset_preview = preview_df.where(
                pd.notnull(preview_df), None
            ).to_dict(orient="records")
        else:
            dataset_preview = []

        # Column stats
        if self._agent_df is not None:
            column_stats: Dict[str, Any] = {}
            for col in self._agent_df.columns:
                s = self._agent_df[col]
                null_count = int(s.isnull().sum())
                null_pct = round(float(s.isnull().mean()), 4)
                dtype = str(s.dtype)
                unique_count = int(s.nunique())
                sample_vals = s.dropna().head(3).tolist()
                try:
                    min_val = float(s.min()) if pd.api.types.is_numeric_dtype(s) else None
                    max_val = float(s.max()) if pd.api.types.is_numeric_dtype(s) else None
                except Exception:
                    min_val, max_val = None, None
                column_stats[col] = {
                    "null_count": null_count,
                    "null_pct": null_pct,
                    "dtype": dtype,
                    "unique_count": unique_count,
                    "sample_values": [str(v) for v in sample_vals],
                    "min_val": min_val,
                    "max_val": max_val,
                }
        else:
            column_stats = {}

        # Issue hints
        issue_hints: List[str] = []
        if self._agent_df is not None and self._clean_df is not None:
            for col in self._agent_df.columns:
                null_pct = self._agent_df[col].isnull().mean()
                if null_pct > 0.01:
                    issue_hints.append(
                        f"Column '{col}' has {null_pct:.1%} missing values"
                    )

            dup_count = int(self._agent_df.duplicated().sum())
            if dup_count > 0:
                issue_hints.append(f"Dataset has {dup_count} duplicate rows")

            for col in self._agent_df.select_dtypes(include=["object"]).columns:
                if col in self._clean_df.columns:
                    clean_vals = set(
                        self._clean_df[col].dropna().astype(str).str.strip()
                    )
                    agent_vals = set(
                        self._agent_df[col].dropna().astype(str).str.strip()
                    )
                    extra_vals = agent_vals - clean_vals
                    if extra_vals:
                        sample = list(extra_vals)[:3]
                        issue_hints.append(
                            f"Column '{col}' has unexpected values: {sample}"
                        )

        task_desc = self._task_config.get("description", "Clean the dataset.")

        return DQAObservation(
            done=done,
            reward=reward,
            task_id=self._task_id,
            task_description=task_desc,
            dataset_preview=dataset_preview,
            column_stats=column_stats,
            issue_hints=issue_hints,
            quality_scores=self._current_scores,
            step_count=self._step_count,
            max_steps=self._max_steps,
            action_history=self._action_history.copy(),
            last_action_result=last_action_result,
            last_action_success=last_action_success,
        )

    @property
    def state(self) -> State:
        """Returns the current internal DQAState for this episode."""
        return DQAState(
            episode_id=self._episode_id,
            step_count=self._step_count,
            task_id=self._task_id,
            current_quality_scores=self._current_scores,
            actions_taken=self._step_count,
            episode_reward=round(self._episode_reward, 4),
            is_submitted=self._is_submitted,
            task_completed=self._is_submitted,
        )


if __name__ == "__main__":
    env = DqaOpenenvEnvironment()

    print("=== ENVIRONMENT SMOKE TEST ===\n")

    for task_id in ["easy", "medium", "hard"]:
        print(f"--- Testing task: {task_id} ---")
        obs = env.reset(task_id)
        print(f"Reset OK: task={obs.task_id}, max_steps={obs.max_steps}")
        print(f"Issues detected: {len(obs.issue_hints)}")
        print(f"Initial scores: {obs.quality_scores}")

        # Step 1: noop
        action = DQAAction(action_type="noop")
        obs = env.step(action)
        print(f"After noop: reward={obs.reward}, done={obs.done}")

        # Step 2: fill_nulls on first null column
        null_cols = [k for k, v in obs.column_stats.items()
                     if v["null_count"] > 0]
        if null_cols:
            action = DQAAction(
                action_type="fill_nulls",
                column=null_cols[0],
                parameters={"strategy": "mean"}
            )
            obs = env.step(action)
            print(f"After fill_nulls on '{null_cols[0]}': reward={obs.reward}")

        # Final: submit
        action = DQAAction(action_type="submit")
        obs = env.step(action)
        print(f"After submit: done={obs.done}, reward={obs.reward}")

        # Check state
        state = env.state
        print(f"State: steps={state.step_count}, submitted={state.is_submitted}")
        print(f"Episode reward total: {state.episode_reward}")
        print()

    print("=== SMOKE TEST COMPLETE ===")