from pydantic import BaseModel
from datetime import datetime
from typing import List, Optional

class ReceiptRequest(BaseModel):
    id: str
    file_name: str
    upload_date: datetime
    receipt_date: datetime
    amount: float
    tax: float
    vendor: str
    category: str

class TransactionRequest(BaseModel):
    id: str
    transaction_date: datetime
    amount: float
    vendor: str
    notes: str

# New: QuickBooks specific models
class QuickBooksTransaction(BaseModel):
    """Raw QuickBooks transaction data"""
    id: str
    txn_date: str  # QuickBooks date format
    amount: float
    payee_name: str  # QuickBooks vendor field
    memo: Optional[str] = None
    account_name: Optional[str] = None
    txn_type: Optional[str] = None

class QuickBooksImportRequest(BaseModel):
    """Request to import QuickBooks transactions"""
    transactions: List[QuickBooksTransaction]
    date_range: Optional[dict] = None
    account_filter: Optional[str] = None

class QuickBooksImportResponse(BaseModel):
    """Response from QuickBooks import"""
    imported_count: int
    converted_transactions: List[TransactionRequest]
    errors: List[str] = []

class MatchResponse(BaseModel):
    receipt_id: str
    transaction_id: str
    confidence_score: float
    match_reason: str
    receipt_vendor: str
    receipt_amount: float
    transaction_vendor: str
    transaction_amount: float

class MatchingRequest(BaseModel):
    receipts: List[ReceiptRequest]
    transactions: List[TransactionRequest]

class MatchingResponse(BaseModel):
    matches: List[MatchResponse]
    stats: dict

class ApprovalRequest(BaseModel):
    match_id: str
    user_id: str
    action: str  # "approve" or "reject"
    reason: Optional[str] = None

class RuleRequest(BaseModel):
    name: str
    condition: str
    action: str
    source: str = "user"

class DocumentUploadResponse(BaseModel):
    file_id: str
    filename: str
    file_type: str
    upload_date: datetime
    status: str

class DocumentProcessResponse(BaseModel):
    file_id: str
    extraction_success: bool
    vendor: Optional[str] = None
    total_amount: Optional[float] = None
    tax_amount: Optional[float] = None
    date: Optional[str] = None
    category: Optional[str] = None
    confidence: Optional[float] = None
    error: Optional[str] = None

class DriveSyncRequest(BaseModel):
    folder_id: Optional[str] = None
    auto_process: bool = True

class DriveSyncResponse(BaseModel):
    files_processed: int
    successful_extractions: int
    failed_extractions: int
    results: List[DocumentProcessResponse] 