"""
Dignity Receipt Schemas

Constitutional dignity tracking and enforcement receipts.
"""

from typing import Any, Dict, Optional

from pydantic import Field

from receipts.base import BaseReceipt


class DignityFloorReceipt(BaseReceipt):
    """
    Receipt emitted when an action violates the constitutional dignity floor.

    The dignity floor (default 0.15) is the absolute minimum dignity threshold
    that all actions must maintain. Violations trigger repair workflows.
    """

    receipt_type: str = Field(default="dignity_floor_violation")

    # Violation details
    measured_dignity: float = Field(..., description="Dignity score of the action")
    floor: float = Field(default=0.15, description="Constitutional floor threshold")

    violation_context: Dict[str, Any] = Field(
        ..., description="Context of the violating action"
    )

    # Repair guidance
    repair_suggestion: str = Field(
        ..., description="Specific repair suggestion for this violation"
    )

    # Resolution tracking
    resolved: bool = Field(default=False, description="Has violation been resolved")
    resolved_by: Optional[str] = Field(
        None, description="Guardian or system that resolved violation"
    )
    resolved_at: Optional[str] = Field(None, description="ISO timestamp of resolution")


class EVHDCorrelationReceipt(BaseReceipt):
    """
    Receipt for EVHD (Emergent Value from Human Dignity) correlation computation.

    Tracks empirical evidence for the hypothesis:
    "Respecting dignity increases emergent value"

    Emitted weekly with correlation statistics between dignity deltas
    and outcome improvements.
    """

    receipt_type: str = Field(default="evhd_correlation")

    # Correlation statistics
    correlation_r: float = Field(..., description="Pearson correlation coefficient")
    correlation_p: float = Field(..., description="Statistical significance (p-value)")
    sample_size: int = Field(..., description="Number of (ΔD, outcome) pairs")

    # Interpretation
    is_significant: bool = Field(..., description="p < 0.05")
    is_positive: bool = Field(..., description="r > 0")
    strength: str = Field(..., description="weak/moderate/strong")

    # Observation window
    window_start: str = Field(..., description="Start of observation period (ISO)")
    window_end: str = Field(..., description="End of observation period (ISO)")

    # Summary statistics
    mean_delta_d: float = Field(..., description="Average dignity change")
    mean_outcome_lift: float = Field(..., description="Average outcome improvement")
    dignity_positive_rate: float = Field(
        ..., description="Proportion of decisions that increased dignity"
    )

    # Hypothesis status
    hypothesis_status: str = Field(
        ..., description="SUPPORTED | UNSUPPORTED | INSUFFICIENT_DATA"
    )

    notes: Optional[str] = None


class DignityBudgetRefusalReceipt(BaseReceipt):
    """
    Receipt emitted when a dignity budget is exhausted.

    From dignity_ledger.py - tracks budget enforcement separate from
    constitutional floor enforcement.
    """

    receipt_type: str = Field(default="dignity_budget_refusal")

    actor: str = Field(..., description="Actor whose budget was exhausted")
    role: str = Field(..., description="Role context for the action")
    action_type: str = Field(..., description="Type of action attempted")

    requested_dignity: float = Field(..., description="Dignity cost of action")
    available_budget: float = Field(..., description="Remaining dignity budget")

    refusal_reason: str = Field(
        ..., description="Explanation of why action was refused"
    )
