"""
synth.py — generate a synthetic 'cheque' image so the OpenCV preprocessing is
demonstrable and testable offline (no real scans needed). We draw horizontal
text lines on white and optionally rotate by a known angle to simulate a skewed
scan/photo — then `preprocess.deskew` should straighten it.
"""
from __future__ import annotations

import cv2
import numpy as np


def create_cheque_image(angle: float = 0.0, width: int = 600, height: int = 280) -> np.ndarray:
    """White cheque with black horizontal text lines + a signature box. `angle`
    (degrees) rotates it to simulate skew. Text is Latin (OpenCV's built-in font
    can't render Arabic) — the Arabic logic is exercised separately in arabic.py."""
    img = np.full((height, width, 3), 255, np.uint8)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, "PAY: Amal Al Mansoori", (20, 60), font, 0.7, (0, 0, 0), 2)
    cv2.putText(img, "Amount in words: one thousand two hundred fifty", (20, 110),
                font, 0.6, (0, 0, 0), 2)
    cv2.putText(img, "Figures: 1,250.00 AED", (20, 160), font, 0.7, (0, 0, 0), 2)
    cv2.putText(img, "Date: 2026-06-14", (20, 210), font, 0.6, (0, 0, 0), 2)
    cv2.rectangle(img, (420, 180), (580, 240), (0, 0, 0), 1)  # signature box
    cv2.putText(img, "sign", (450, 220), font, 0.6, (0, 0, 0), 1)
    if abs(angle) > 1e-6:
        M = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1.0)
        img = cv2.warpAffine(img, M, (width, height),
                             borderValue=(255, 255, 255))
    return img


# A FakeOCR result for the cheque — stands in for Azure AI Vision Read / GPT-4o
# vision, which we can't run offline. The figures field uses Eastern Arabic
# numerals to exercise normalization; the words field is Arabic.
CHEQUE_OCR = {
    "payee": "Amal Al Mansoori",
    "amount_words_ar": "ألف ومئتان وخمسون درهم",      # 1250
    "amount_figures": "١٬٢٥٠٫٠٠",                       # Eastern numerals -> 1250.00
    "date": "٢٠٢٦-٠٦-١٤",
    "signature_present": True,
}

# A fraudulent/erroneous cheque: words say 1250 but figures say 1350 (mismatch).
CHEQUE_OCR_MISMATCH = dict(CHEQUE_OCR, amount_figures="١٬٣٥٠٫٠٠")
