#!/usr/bin/env python3

# Database seeding script
# This script populates the database with initial data for testing and development

import sys
import os

# Add the parent directory to the Python path so we can import our modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from database_helper import db
from user_stuff import user_manager
from workspace_manager import WorkspaceManager
import json
import datetime

def seed_database():
    """Seed the database with initial data"""
    print("Starting database seeding...")
    
    # Connect to database
    if not db.connect():
        print("Failed to connect to database")
        return False
    
    print("Connected to database successfully")
    
    # Create sample users
    print("Creating sample users...")
    
    users_data = [
        {
            "username": "admin",
            "email": "admin@example.com",
            "password": "admin123",
            "confirm_password": "admin123"
        },
        {
            "username": "testuser",
            "email": "test@example.com", 
            "password": "password123",
            "confirm_password": "password123"
        },
        {
            "username": "demouser",
            "email": "demo@example.com",
            "password": "demo2023",
            "confirm_password": "demo2023"
        },
        {
            "username": "alice",
            "email": "alice@company.com",
            "password": "alicepass",
            "confirm_password": "alicepass"
        },
        {
            "username": "bob",
            "email": "bob@company.com",
            "password": "bobsecret",
            "confirm_password": "bobsecret"
        }
    ]
    
    created_users = []
    for user_data in users_data:
        result = user_manager.register_user(
            user_data["username"],
            user_data["email"], 
            user_data["password"],
            user_data["confirm_password"]
        )
        if result["success"]:
            created_users.append({
                "id": result["user_id"],
                "username": user_data["username"],
                "email": user_data["email"]
            })
            print(f"✓ Created user: {user_data['username']}")
        else:
            print(f"✗ Failed to create user {user_data['username']}: {result['error']}")
    
    # Create sample workspaces
    print("\nCreating sample workspaces...")
    ws_manager = WorkspaceManager()
    
    workspaces_data = [
        {
            "name": "Development Team",
            "description": "Main development workspace for project collaboration",
            "owner": "admin"
        },
        {
            "name": "Design Assets", 
            "description": "Workspace for design documents and mockups",
            "owner": "alice"
        },
        {
            "name": "Documentation",
            "description": "Technical documentation and user guides",
            "owner": "testuser"
        }
    ]
    
    for ws_data in workspaces_data:
        # Find owner user ID
        owner_user = next((u for u in created_users if u["username"] == ws_data["owner"]), None)
        if owner_user:
            result = ws_manager.create_workspace(
                ws_data["name"],
                owner_user["id"], 
                ws_data["description"]
            )
            if result and result.get("success"):
                print(f"✓ Created workspace: {ws_data['name']}")
            else:
                print(f"✗ Failed to create workspace: {ws_data['name']}")
    
    # Create sample documents
    print("\nCreating sample documents...")
    from document_things import DocumentManager
    doc_manager = DocumentManager()
    
    documents_data = [
        {
            "title": "Getting Started Guide",
            "content": "Welcome to our collaborative workspace! This guide will help you get started with creating and managing documents.",
            "author": "admin"
        },
        {
            "title": "Project Roadmap",
            "content": "# Project Roadmap\n\n## Q1 2024\n- Feature planning\n- Initial development\n\n## Q2 2024\n- Beta testing\n- User feedback",
            "author": "alice"
        },
        {
            "title": "Meeting Notes - Week 1", 
            "content": "## Team Meeting Notes\n\n**Date:** Today\n\n**Attendees:** Team members\n\n**Topics:**\n- Project kickoff\n- Task assignments\n- Timeline review",
            "author": "testuser"
        }
    ]
    
    for doc_data in documents_data:
        author_user = next((u for u in created_users if u["username"] == doc_data["author"]), None)
        if author_user:
            # Get user's default workspace
            workspaces = user_manager.get_user_workspaces(author_user["id"])
            if workspaces:
                workspace_id = workspaces[0]["id"]
                result = doc_manager.create_document(
                    doc_data["title"],
                    doc_data["content"],
                    workspace_id,
                    author_user["id"]
                )
                if result and result.get("success"):
                    print(f"✓ Created document: {doc_data['title']}")
                else:
                    print(f"✗ Failed to create document: {doc_data['title']}")
    
    print(f"\nDatabase seeding completed successfully!")
    print(f"Created {len(created_users)} users, {len(workspaces_data)} workspaces, and {len(documents_data)} documents")
    
    # Display login credentials for reference
    print("\n" + "="*50)
    print("SAMPLE USER CREDENTIALS:")
    print("="*50)
    for user_data in users_data[:3]:  # Show first 3 users
        print(f"Username: {user_data['username']}")
        print(f"Password: {user_data['password']}")
        print("-" * 20)
    
    db.close()
    return True

if __name__ == "__main__":
    success = seed_database()
    if not success:
        sys.exit(1)