"""
=========================================
CV6 AI Trading OS
Idempotency record — SQLAlchemy ORM model
PHASE-2.5 FIX: idempotency state must survive a server restart. The
previous in-memory-only cache (app/execution/idempotency.py) was wiped
on every process restart, defeating duplicate-order protection for any
retry that happened to arrive after a restart.
=========================================
"""

import time
from sqlalchemy import Column, String, Float, Text
from app.database.base import Base


class IdempotencyRecord(Base):
    """
    One row per dedup key (explicit caller-supplied key, or an implicit
    order fingerprint). Stores the exact response that was returned the
    first time, so a repeat request within the TTL window gets the same
    response replayed instead of placing a second real order.
    """
    __tablename__ = "idempotency_records"

    key           = Column(String(255), primary_key=True)
    response_json = Column(Text, nullable=False)
    created_at    = Column(Float, default=lambda: time.time(), index=True)
