"""
Create test users for end-to-end workflow testing
This script creates contractor and worker accounts with all required fields
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import create_app
from app.extensions import db
from app.models.user import User
from app.models.category import Category
from datetime import datetime, timezone

def create_test_users():
    """Create test users for workflow testing"""
    app = create_app()
    with app.app_context():
        print("Creating test users for workflow...")
        
        # Check if test users already exist
        contractor = User.query.filter_by(email="contractor@test.com").first()
        worker = User.query.filter_by(email="worker@test.com").first()
        
        if contractor:
            print("Contractor test user already exists")
        else:
            # Create contractor user
            contractor = User(
                email="contractor@test.com",
                first_name="Test",
                last_name="Contractor",
                role="contractor",
                phone_number="0412345678",
                location="Sydney, NSW",
                abn_number="12345678901",
                business_name="Test Construction Co",
                primary_trade="General Construction",
                gst_registered=True,
                public_liability_insurance=True,
                public_liability_amount=5000000,
                is_active=True,
                privacy_consent=True,
                terms_accepted=True,
                terms_accepted_date=datetime.now(timezone.utc)
            )
            contractor.set_password("password123")
            db.session.add(contractor)
            print("✓ Created contractor: contractor@test.com / password123")
        
        if worker:
            print("Worker test user already exists")
        else:
            # Create worker user
            worker = User(
                email="worker@test.com",
                first_name="Test",
                last_name="Worker",
                role="worker",
                phone_number="0498765432",
                location="Melbourne, VIC",
                abn_number="98765432109",
                business_name="Test Worker Services",
                primary_trade="Carpentry",
                gst_registered=True,
                white_card_number="WC123456",
                public_liability_insurance=True,
                public_liability_amount=2000000,
                is_active=True,
                privacy_consent=True,
                terms_accepted=True,
                terms_accepted_date=datetime.now(timezone.utc)
            )
            worker.set_password("password123")
            db.session.add(worker)
            print("✓ Created worker: worker@test.com / password123")
        
        # Ensure categories exist
        categories_exist = Category.query.count() > 0
        if not categories_exist:
            print("Creating default categories...")
            categories = [
                Category(name="General Construction", description="General construction work", whs_risk_level="medium", is_active=True, sort_order=1),
                Category(name="Electrical", description="Electrical installation and repair", whs_risk_level="high", is_active=True, sort_order=2),
                Category(name="Plumbing", description="Plumbing installation and repair", whs_risk_level="medium", is_active=True, sort_order=3),
                Category(name="Carpentry", description="Carpentry and woodwork", whs_risk_level="low", is_active=True, sort_order=4),
                Category(name="Painting", description="Interior and exterior painting", whs_risk_level="low", is_active=True, sort_order=5),
            ]
            for cat in categories:
                db.session.add(cat)
            print("✓ Created 5 default categories")
        
        # Commit all changes
        try:
            db.session.commit()
            print("\n✅ Test users ready for workflow testing!")
            print("\nLogin credentials:")
            print("- Contractor: contractor@test.com / password123")
            print("- Worker: worker@test.com / password123")
        except Exception as e:
            db.session.rollback()
            print(f"❌ Error creating test users: {e}")

if __name__ == "__main__":
    create_test_users()
