"""
Type definitions for Temporal workflows.

These types are shared between:
- Workflows (define the contract)
- Clients (start workflows, send signals)
- Platform worker activities (receive as input)
"""

from dataclasses import dataclass


# ============================================================================
# KYC Workflow Types
# ============================================================================


@dataclass
class KycData:
    """Input data for KYC workflow."""

    kyc_request_id: str
    team_name: str
    owner_id: str
    owner_email: str
    country_code: str = ""
    country_data: dict | None = None
    # team_id создаётся в workflow после approve


@dataclass
class KycWorkflowResult:
    """Output of KYC workflow."""

    status: str  # "approved" | "rejected"
    kyc_request_id: str
    team_id: str = ""  # Создаётся при approve
    logto_org_id: str = ""
    reason: str = ""


@dataclass
class CreateTeamInput:
    """Input for create_team_in_teams activity."""

    team_name: str
    owner_id: str
    team_type: str = "BUYER"  # BUYER | SELLER
    country_code: str = ""


@dataclass
class CreateTeamResult:
    """Result of create_team_in_teams activity."""

    success: bool
    team_id: str = ""
    team_uuid: str = ""
    message: str = ""


@dataclass
class KycApprovalData:
    """Signal data for KYC approval."""

    approved_by: str
    notes: str = ""


@dataclass
class KycRejectionData:
    """Signal data for KYC rejection."""

    rejected_by: str
    reason: str


# ============================================================================
# Activity I/O Types (used by platform worker)
# ============================================================================


@dataclass
class CreateLogtoOrgInput:
    """Input for create_logto_org activity."""

    team_id: str
    team_name: str
    owner_id: str


@dataclass
class CreateLogtoOrgResult:
    """Result of create_logto_org activity."""

    team_id: str
    organization_id: str
    created: bool


@dataclass
class AddKycToOdooResult:
    """Result of add_kyc_to_odoo activity."""

    success: bool
    odoo_application_id: int | None = None
    application_name: str = ""
    message: str = ""


@dataclass
class LogKycEventInput:
    """Input for log_kyc_event activity."""

    kyc_id: str
    user_id: str
    event: str
    description: str


@dataclass
class LogKycEventResult:
    """Result of log_kyc_event activity."""

    success: bool
    message: str = ""


@dataclass
class SetBuyerLogtoOrgInput:
    """Input for set_buyer_logto_org activity."""

    team_uuid: str
    logto_org_id: str


@dataclass
class SetBuyerLogtoOrgResult:
    """Result of set_buyer_logto_org activity."""

    success: bool
    buyer_uuid: str = ""
    message: str = ""


@dataclass
class SetLogtoOrgIdInput:
    """Input for set_logto_org_id activity."""

    team_id: str
    logto_org_id: str


@dataclass
class SetLogtoOrgIdResult:
    """Result of set_logto_org_id activity."""

    team_id: str
    logto_org_id: str
    updated: bool


# ============================================================================
# Offer Workflow Types
# ============================================================================


@dataclass
class OfferData:
    """Input data for offer creation workflow."""

    offer_uuid: str
    team_uuid: str
    product_uuid: str
    product_name: str
    category_name: str | None = None
    location_uuid: str | None = None
    location_name: str | None = None
    location_country: str | None = None
    location_country_code: str | None = None
    location_latitude: float | None = None
    location_longitude: float | None = None
    quantity: str | None = None
    unit: str | None = None
    price_per_unit: str | None = None
    currency: str | None = None
    description: str | None = None
    valid_until: str | None = None
    terminus_schema_id: str | None = None
    terminus_payload: dict | None = None


@dataclass
class OfferWorkflowResult:
    """Output of offer workflow."""

    status: str  # completed | failed
    offer_uuid: str
    message: str = ""


@dataclass
class CreateOfferInExchangeInput:
    """Input for create_offer_in_exchange activity."""

    offer_uuid: str
    team_uuid: str
    product_uuid: str
    product_name: str
    category_name: str | None = None
    location_uuid: str | None = None
    location_name: str | None = None
    location_country: str | None = None
    location_country_code: str | None = None
    location_latitude: float | None = None
    location_longitude: float | None = None
    quantity: str | None = None
    unit: str | None = None
    price_per_unit: str | None = None
    currency: str | None = None
    description: str | None = None
    valid_until: str | None = None
    terminus_schema_id: str | None = None
    terminus_document_id: str | None = None


@dataclass
class CreateOfferInExchangeResult:
    """Result of create_offer_in_exchange activity."""

    success: bool
    offer_uuid: str
    message: str = ""


@dataclass
class UpdateOfferWorkflowStatusInput:
    """Input for update_offer_workflow_status activity."""

    offer_uuid: str
    status: str  # pending | active | error
    error_message: str | None = None


@dataclass
class UpdateOfferWorkflowStatusResult:
    """Result of update_offer_workflow_status activity."""

    success: bool
    message: str = ""


@dataclass
class SaveOfferDescriptionInput:
    """Input for save_offer_description activity."""

    offer_uuid: str
    schema_id: str
    payload: dict


@dataclass
class SaveOfferDescriptionResult:
    """Result of save_offer_description activity."""

    success: bool
    document_id: str = ""
    message: str = ""


@dataclass
class TriggerPrefectOfferSyncInput:
    """Input for trigger_prefect_offer_sync activity."""

    offer_uuid: str
    location_uuid: str
    location_latitude: float
    location_longitude: float
    team_uuid: str
    product_uuid: str
    price_per_unit: str | None = None
    currency: str | None = None
    quantity: str | None = None
    unit: str | None = None


@dataclass
class TriggerPrefectOfferSyncResult:
    """Result of trigger_prefect_offer_sync activity."""

    success: bool
    flow_run_id: str = ""
    message: str = ""


# ============================================================================
# Invitation Workflow Types
# ============================================================================


@dataclass
class InvitationData:
    """Input data for invitation workflow."""

    team_uuid: str
    email: str
    role: str
    invited_by: str
    expires_at: str


@dataclass
class InvitationWorkflowResult:
    """Output of invitation workflow."""

    status: str  # completed | failed
    invitation_uuid: str = ""
    token: str = ""
    message: str = ""


@dataclass
class CreateInvitationInput:
    """Input for create_invitation activity."""

    team_uuid: str
    email: str
    role: str
    invited_by: str
    expires_at: str


@dataclass
class CreateInvitationResult:
    """Result of create_invitation activity."""

    success: bool
    invitation_uuid: str = ""
    message: str = ""


@dataclass
class CreateInvitationTokenInput:
    """Input for create_invitation_token activity."""

    invitation_uuid: str
    expires_at: str


@dataclass
class CreateInvitationTokenResult:
    """Result of create_invitation_token activity."""

    success: bool
    token: str = ""
    token_uuid: str = ""
    message: str = ""


@dataclass
class UpdateInvitationTokenStatusInput:
    """Input for update_invitation_token_status activity."""

    token_uuid: str
    status: str  # pending | active | error


@dataclass
class UpdateInvitationTokenStatusResult:
    """Result of update_invitation_token_status activity."""

    success: bool
    message: str = ""


@dataclass
class SendInviteNotificationInput:
    """Input for send_invite_notification activity."""

    email: str
    team_uuid: str
    role: str
    token: str


@dataclass
class SendInviteNotificationResult:
    """Result of send_invite_notification activity."""

    success: bool
    message: str = ""


# ============================================================================
# KYC Django Activity Types
# ============================================================================


@dataclass
class CreateKycInDjangoInput:
    """Input for create_kyc_in_django activity."""

    workflow_id: str
    user_id: str
    team_uuid: str
    team_name: str
    country_code: str
    contact_person: str
    contact_email: str
    contact_phone: str
    country_data: dict  # Специфичные для страны данные (inn, ogrn и т.д.)


@dataclass
class CreateKycInDjangoResult:
    """Result of create_kyc_in_django activity."""

    success: bool
    kyc_request_uuid: str = ""
    message: str = ""


# ============================================================================
# Address Workflow Types
# ============================================================================


@dataclass
class AddressData:
    """Input data for Address workflow."""

    workflow_id: str
    team_uuid: str
    name: str
    address: str
    latitude: float | None = None
    longitude: float | None = None
    country_code: str | None = None
    is_default: bool = False


@dataclass
class AddressWorkflowResult:
    """Output of Address workflow."""

    status: str  # "completed" | "failed"
    address_uuid: str = ""
    graph_node_id: str = ""
    message: str = ""


@dataclass
class CreateAddressInDjangoInput:
    """Input for create_address_in_django activity."""

    workflow_id: str
    team_uuid: str
    name: str
    address: str
    latitude: float | None = None
    longitude: float | None = None
    country_code: str | None = None
    is_default: bool = False


@dataclass
class CreateAddressInDjangoResult:
    """Result of create_address_in_django activity."""

    success: bool
    address_uuid: str = ""
    team_type: str = ""  # "buyer" or "seller"
    message: str = ""


@dataclass
class TriggerPrefectSyncInput:
    """Input for trigger_prefect_sync activity."""

    node_uuid: str
    name: str | None = None
    latitude: float | None = None
    longitude: float | None = None
    country_code: str | None = None
    node_type: str | None = None  # "logistics" | "buyer" | "seller"
    team_uuid: str | None = None  # For buyer/seller nodes


@dataclass
class TriggerPrefectSyncResult:
    """Result of trigger_prefect_sync activity."""

    success: bool
    flow_run_id: str = ""
    message: str = ""


@dataclass
class UpdateAddressStatusInput:
    """Input for update_address_status activity."""

    address_uuid: str
    status: str  # "pending" | "active" | "error"
    error_message: str | None = None


@dataclass
class UpdateAddressStatusResult:
    """Result of update_address_status activity."""

    success: bool
    message: str = ""
