#!/usr/bin/env python3
"""
Script to add the 'accountant' role to the database.
Run this script once to create the accountant role if it doesn't exist.
"""

import sys
from pathlib import Path

# Add the Backend directory to the path
backend_dir = Path(__file__).parent
sys.path.insert(0, str(backend_dir))

from src.shared.config.database import SessionLocal
from src.models.role import Role

def add_accountant_role():
    """Add the accountant role to the database if it doesn't exist."""
    db = SessionLocal()
    try:
        # Check if accountant role already exists
        existing_role = db.query(Role).filter(Role.name == 'accountant').first()
        
        if existing_role:
            print("✓ Accountant role already exists in the database.")
            print(f"  Role ID: {existing_role.id}")
            print(f"  Role Name: {existing_role.name}")
            return
        
        # Create the accountant role
        accountant_role = Role(
            name='accountant',
            description='Accountant role with access to financial data, payments, and invoices'
        )
        db.add(accountant_role)
        db.commit()
        db.refresh(accountant_role)
        
        print("✓ Accountant role created successfully!")
        print(f"  Role ID: {accountant_role.id}")
        print(f"  Role Name: {accountant_role.name}")
        print(f"  Description: {accountant_role.description}")
        
    except Exception as e:
        db.rollback()
        print(f"✗ Error creating accountant role: {e}")
        sys.exit(1)
    finally:
        db.close()

if __name__ == '__main__':
    print("Adding accountant role to the database...")
    print("-" * 50)
    add_accountant_role()
    print("-" * 50)
    print("Done!")

