#!/usr/bin/env python
"""
Create COGS (Cost of Goods Sold) account if it doesn't exist
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.database import SessionLocal
from app.models.accounting import AccountingCode
from uuid import uuid4

db = SessionLocal()

print("=" * 80)
print("CREATE COGS ACCOUNT")
print("=" * 80)

# Check if COGS account already exists
cogs = db.query(AccountingCode).filter(
    AccountingCode.code == '5000'
).first()

if cogs:
    print(f"\n✓ COGS account already exists: {cogs.code} - {cogs.name}")
else:
    print("\nCreating COGS account...")

    # Create new COGS account
    cogs_account = AccountingCode(
        id=str(uuid4()),
        code='5000',
        name='Cost of Goods Sold',
        account_type='Expense',
        category='Operating Expenses',
        is_parent=False,
        total_debits=0.0,
        total_credits=0.0,
        balance=0.0,
        currency='USD',
        reporting_tag='Cost of Sales',
        is_dimensional_template=True,
        hierarchy_level=1,
        sort_order=5000
    )

    db.add(cogs_account)
    db.commit()

    print(f"\n✓ Created COGS account: {cogs_account.code} - {cogs_account.name}")
    print(f"  ID: {cogs_account.id}")

# Verify account
cogs_final = db.query(AccountingCode).filter(
    AccountingCode.code == '5000'
).first()

if cogs_final:
    print(f"\n✓ VERIFICATION SUCCESS: Account {cogs_final.code} exists in database")
else:
    print("\n✗ VERIFICATION FAILED: Could not create COGS account")
    sys.exit(1)

print("\n" + "=" * 80)
print("COGS ACCOUNT CREATION COMPLETE")
print("=" * 80)

db.close()
