"""
SCM Domain Exceptions.

استثناهای حوزه خرید و تدارکات.
"""


class SCMDomainError(Exception):
    """خطای پایه ماژول SCM."""
    pass


# ─── Vendor Exceptions ────────────────────────────────────────

class DuplicateVendorCodeError(SCMDomainError):
    def __init__(self, code: str):
        super().__init__(f"Vendor code '{code}' already exists")
        self.code = code


class VendorNotFoundError(SCMDomainError):
    def __init__(self, vendor_id=None, vendor_code=None):
        identifier = vendor_code or str(vendor_id)
        super().__init__(f"Vendor '{identifier}' not found")


class VendorBlockedError(SCMDomainError):
    def __init__(self, vendor_code: str):
        super().__init__(f"Vendor '{vendor_code}' is blocked")
        self.vendor_code = vendor_code


class VendorInactiveError(SCMDomainError):
    def __init__(self, vendor_code: str):
        super().__init__(f"Vendor '{vendor_code}' is inactive")


# ─── PR Exceptions ────────────────────────────────────────────

class PRNotFoundError(SCMDomainError):
    def __init__(self, pr_id=None):
        super().__init__(f"Purchase Requisition '{pr_id}' not found")


class DuplicatePRNumberError(SCMDomainError):
    def __init__(self, pr_number: str):
        super().__init__(f"PR number '{pr_number}' already exists")


class PRInvalidStatusError(SCMDomainError):
    def __init__(self, pr_number: str, current_status: str, expected_status: str):
        super().__init__(
            f"PR '{pr_number}' status is '{current_status}', expected '{expected_status}'"
        )


# ─── RFQ Exceptions ───────────────────────────────────────────

class RFQNotFoundError(SCMDomainError):
    def __init__(self, rfq_id=None):
        super().__init__(f"RFQ '{rfq_id}' not found")


class DuplicateRFQNumberError(SCMDomainError):
    def __init__(self, rfq_number: str):
        super().__init__(f"RFQ number '{rfq_number}' already exists")


# ─── PO Exceptions ────────────────────────────────────────────

class PONotFoundError(SCMDomainError):
    def __init__(self, po_id=None):
        super().__init__(f"Purchase Order '{po_id}' not found")


class DuplicatePONumberError(SCMDomainError):
    def __init__(self, po_number: str):
        super().__init__(f"PO number '{po_number}' already exists")


class POInvalidStatusError(SCMDomainError):
    def __init__(self, po_number: str, current_status: str, expected_status: str):
        super().__init__(
            f"PO '{po_number}' status is '{current_status}', expected '{expected_status}'"
        )


class PurchaseLimitExceededError(SCMDomainError):
    def __init__(self, amount, limit):
        super().__init__(
            f"Purchase amount {amount} exceeds limit {limit}"
        )
        self.amount = amount
        self.limit = limit


# ─── GR Exceptions ────────────────────────────────────────────

class GRNotFoundError(SCMDomainError):
    def __init__(self, gr_id=None):
        super().__init__(f"Goods Receipt '{gr_id}' not found")


class OverReceiptError(SCMDomainError):
    def __init__(self, item_code: str, ordered: str, receiving: str):
        super().__init__(
            f"Over-receipt for item '{item_code}': ordered={ordered}, receiving={receiving}"
        )


# ─── Return Exceptions ───────────────────────────────────────

class ReturnNotFoundError(SCMDomainError):
    def __init__(self, return_id=None):
        super().__init__(f"Purchase Return '{return_id}' not found")


class OverReturnError(SCMDomainError):
    def __init__(self, item_code: str, received: str, returning: str):
        super().__init__(
            f"Over-return for item '{item_code}': received={received}, returning={returning}"
        )


# ─── Evaluation Exceptions ───────────────────────────────────

class EvaluationNotFoundError(SCMDomainError):
    def __init__(self, evaluation_id=None):
        super().__init__(f"Vendor Evaluation '{evaluation_id}' not found")


class DuplicateEvaluationError(SCMDomainError):
    def __init__(self, vendor_id, period_id):
        super().__init__(
            f"Evaluation already exists for vendor '{vendor_id}' in period '{period_id}'"
        )
