"""
Initialize default invoice template on database startup
"""
from app.core.database import SessionLocal
from app.models.invoice_template import InvoiceTemplate
import uuid
from datetime import datetime


def seed_default_template():
    """Create default template if none exists"""
    db = SessionLocal()
    try:
        # Check if any templates exist
        existing = db.query(InvoiceTemplate).filter(InvoiceTemplate.is_active == True).first()
        if existing:
            print("✓ Invoice templates already exist, skipping seed")
            return

        # Create default template
        default_template = InvoiceTemplate(
            id=uuid.uuid4(),
            name="Default Template",
            description="Default professional invoice template",
            is_default=True,
            is_active=True,
            # Page layout
            page_margin_top=20,
            page_margin_bottom=20,
            page_margin_left=20,
            page_margin_right=20,
            # Logo
            show_logo=True,
            logo_width_mm=80,
            logo_height_mm=80,
            logo_position="left",
            # Company info
            show_company_name=True,
            show_company_address=True,
            show_company_phone=True,
            show_company_email=True,
            show_company_website=True,
            show_company_vat=True,
            # Invoice details
            show_invoice_number=True,
            show_invoice_date=True,
            show_due_date=True,
            show_payment_terms=True,
            show_branch=True,
            show_generated_at=True,
            # Customer info
            show_customer=True,
            show_customer_address=True,
            show_customer_phone=True,
            show_customer_email=True,
            show_customer_vat=True,
            # Items table
            items_columns=[
                {"name": "Product", "width": 80},
                {"name": "Qty", "width": 30},
                {"name": "Unit Price", "width": 40},
                {"name": "Discount", "width": 30},
                {"name": "VAT", "width": 25},
                {"name": "Total", "width": 40},
            ],
            items_header_background="#343a40",
            items_header_text_color="#ffffff",
            items_border_color="#dee2e6",
            items_stripe_color="#f8f9fa",
            items_font_size=12,
            # Totals
            show_totals=True,
            show_subtotal=True,
            show_discount=True,
            show_vat=True,
            show_total=True,
            totals_background_color="#f8f9fa",
            totals_font_size=14,
            # Footer
            show_footer=True,
            show_footer_text=True,
            show_terms_conditions=True,
            show_bank_details=False,
            footer_text="Thank you for your business!",
            footer_background_color="#ffffff",
            footer_font_size=11,
            # Styling
            font_family="Helvetica",
            base_font_size=12,
            primary_color="#007bff",
            secondary_color="#6c757d",
            accent_color="#28a745",
            text_color="#000000",
            border_color="#dee2e6",
        )

        db.add(default_template)
        db.commit()
        print("✓ Default invoice template created successfully")

    except Exception as e:
        print(f"⚠ Error seeding default template: {e}")
        db.rollback()
    finally:
        db.close()


if __name__ == "__main__":
    seed_default_template()
