# next_steps.py
import os
import subprocess

def setup_next_features():
    print("🚀 SETTING UP NEXT FEATURES FOR RAPHA-BETHEL BNPS")
    print("=" * 60)
    
    steps = [
        {
            "title": "1. Enhanced Dashboard with Charts",
            "description": "Add interactive charts for attendance, revenue, and enrollment",
            "status": "✅ Already implemented above"
        },
        {
            "title": "2. Student Portal",
            "description": "Create student list and detail pages with search and filters",
            "status": "📝 Ready to implement"
        },
        {
            "title": "3. Attendance Management",
            "description": "Create daily attendance marking interface",
            "files": [
                "academics/views.py - AttendanceMarkingView",
                "templates/attendance/mark.html",
                "templates/attendance/report.html"
            ]
        },
        {
            "title": "4. Fee Management",
            "description": "Create invoice generation and payment tracking",
            "files": [
                "finance/views.py - InvoiceListView, PaymentView",
                "templates/finance/invoices.html",
                "templates/finance/payments.html"
            ]
        },
        {
            "title": "5. Parent Portal",
            "description": "Create portal for parents to view student progress and fees",
            "files": [
                "parents/views.py - ParentDashboardView",
                "templates/parents/dashboard.html"
            ]
        },
        {
            "title": "6. Reports Module",
            "description": "Generate academic and financial reports",
            "files": [
                "reports/views.py - ReportView",
                "templates/reports/academic.html",
                "templates/reports/financial.html"
            ]
        },
        {
            "title": "7. Mobile Responsive Design",
            "description": "Ensure all pages work perfectly on mobile devices",
            "status": "📱 In progress"
        },
        {
            "title": "8. API Endpoints",
            "description": "Create REST API for potential mobile app integration",
            "files": [
                "api/views.py - API endpoints",
                "api/serializers.py - Data serializers"
            ]
        }
    ]
    
    for step in steps:
        print(f"\n{step['title']}")
        print(f"   {step['description']}")
        if 'status' in step:
            print(f"   Status: {step['status']}")
        if 'files' in step:
            print(f"   Files to create:")
            for file in step['files']:
                print(f"     - {file}")
    
    print("\n" + "=" * 60)
    print("\n📋 QUICK START COMMANDS:")
    print("""
# Create student portal templates
mkdir -p templates/students
touch templates/students/student_list.html
touch templates/students/student_detail.html

# Create attendance templates
mkdir -p templates/attendance
touch templates/attendance/mark.html
touch templates/attendance/report.html

# Create finance templates
mkdir -p templates/finance
touch templates/finance/invoices.html
touch templates/finance/payments.html

# Update URLs
echo '
# Add to backend/urls.py
path("attendance/", include("academics.urls")),
path("finance/", include("finance.urls")),
' >> backend/urls.py
""")

def create_student_portal():
    print("\n🎓 CREATING STUDENT PORTAL TEMPLATES...")
    
    base_dir = os.path.dirname(os.path.abspath(__file__))
    
    # Create student list template
    student_list_content = '''{% extends 'base.html' %}
{% load static %}

{% block title %}Students - Rapha-Bethel BNPS{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0 text-gray-800">Students ({{ total_count }})</h1>
        <div class="btn-group">
            <a href="/admin/students/student/add/" class="btn btn-primary">
                <i class="fas fa-user-plus"></i> Add Student
            </a>
            <button class="btn btn-outline-primary" onclick="window.print()">
                <i class="fas fa-print"></i> Print
            </button>
        </div>
    </div>

    <!-- Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                        Total Students
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">{{ total_count }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                        Active Students
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">{{ active_count }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                        Male Students
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ students|dictsort:"gender"|length }}  <!-- Update with actual count -->
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                        Female Students
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ students|dictsortreversed:"gender"|length }}  <!-- Update with actual count -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Search and Filter -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Search & Filter</h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <input type="text" class="form-control" name="q" placeholder="Search by name or ID...">
                </div>
                <div class="col-md-3">
                    <select class="form-control" name="class">
                        <option value="">All Classes</option>
                        <!-- Add class options dynamically -->
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-control" name="status">
                        <option value="">All Status</option>
                        <option value="active">Active</option>
                        <option value="inactive">Inactive</option>
                        <option value="graduated">Graduated</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search"></i> Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Student Table -->
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Student ID</th>
                            <th>Name</th>
                            <th>Class</th>
                            <th>Gender</th>
                            <th>Admission Date</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for student in students %}
                        <tr>
                            <td>
                                <strong>{{ student.student_id }}</strong>
                            </td>
                            <td>
                                {{ student.first_name }} {{ student.last_name }}<br>
                                <small class="text-muted">{{ student.date_of_birth|date:"d M Y" }}</small>
                            </td>
                            <td>
                                {% if student.current_class %}
                                <span class="badge badge-info">{{ student.current_class }}</span>
                                {% else %}
                                <span class="badge badge-secondary">Not Assigned</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if student.gender == 'M' %}
                                <span class="badge badge-primary">Male</span>
                                {% else %}
                                <span class="badge badge-pink">Female</span>
                                {% endif %}
                            </td>
                            <td>{{ student.admission_date|date:"d M Y" }}</td>
                            <td>
                                {% if student.status == 'active' %}
                                <span class="badge badge-success">Active</span>
                                {% elif student.status == 'graduated' %}
                                <span class="badge badge-secondary">Graduated</span>
                                {% else %}
                                <span class="badge badge-warning">{{ student.status|title }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'student_detail' student.pk %}" class="btn btn-sm btn-info">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="/admin/students/student/{{ student.pk }}/change/" 
                                   class="btn btn-sm btn-warning">
                                    <i class="fas fa-edit"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="text-center py-4">
                                <i class="fas fa-users fa-2x text-muted mb-3"></i>
                                <p class="text-muted">No students found</p>
                                <a href="/admin/students/student/add/" class="btn btn-primary">
                                    <i class="fas fa-user-plus"></i> Add First Student
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            {% if is_paginated %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                    </li>
                    {% endif %}
                    
                    {% for num in page_obj.paginator.page_range %}
                    <li class="page-item {% if page_obj.number == num %}active{% endif %}">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                    </li>
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
'''
    
    student_list_path = os.path.join(base_dir, 'templates', 'students', 'student_list.html')
    os.makedirs(os.path.dirname(student_list_path), exist_ok=True)
    
    with open(student_list_path, 'w', encoding='utf-8') as f:
        f.write(student_list_content)
    print(f"✅ Created: templates/students/student_list.html")
    
    # Create student detail template
    student_detail_content = '''{% extends 'base.html' %}
{% load static %}

{% block title %}{{ student }} - Rapha-Bethel BNPS{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Student Header -->
    <div class="card shadow mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-8">
                    <h2 class="text-primary">{{ student.first_name }} {{ student.last_name }}</h2>
                    <p class="text-muted">
                        Student ID: {{ student.student_id }} • 
                        Class: {{ student.current_class|default:"Not Assigned" }} • 
                        Status: <span class="badge badge-{{ student.status }}">{{ student.status|title }}</span>
                    </p>
                </div>
                <div class="col-md-4 text-right">
                    <a href="/admin/students/student/{{ student.pk }}/change/" class="btn btn-warning">
                        <i class="fas fa-edit"></i> Edit
                    </a>
                    <a href="/admin/students/student/{{ student.pk }}/delete/" class="btn btn-danger">
                        <i class="fas fa-trash"></i> Delete
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Student Information -->
        <div class="col-lg-4">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Student Information</h6>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <td><strong>Date of Birth:</strong></td>
                            <td>{{ student.date_of_birth|date:"d M Y" }}</td>
                        </tr>
                        <tr>
                            <td><strong>Age:</strong></td>
                            <td>{{ student.get_age }} years</td>
                        </tr>
                        <tr>
                            <td><strong>Gender:</strong></td>
                            <td>{{ student.get_gender_display }}</td>
                        </tr>
                        <tr>
                            <td><strong>Admission Date:</strong></td>
                            <td>{{ student.admission_date|date:"d M Y" }}</td>
                        </tr>
                        <tr>
                            <td><strong>Home Address:</strong></td>
                            <td>{{ student.home_address }}</td>
                        </tr>
                        <tr>
                            <td><strong>Quarter:</strong></td>
                            <td>{{ student.quarter }}</td>
                        </tr>
                    </table>
                </div>
            </div>

            <!-- Contact Information -->
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Contact Information</h6>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <td><strong>Phone:</strong></td>
                            <td>{{ student.phone|default:"Not provided" }}</td>
                        </tr>
                        <tr>
                            <td><strong>Emergency Contact:</strong></td>
                            <td>{{ student.emergency_contact|default:"Not provided" }}</td>
                        </tr>
                        <tr>
                            <td><strong>Emergency Name:</strong></td>
                            <td>{{ student.emergency_contact_name|default:"Not provided" }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <!-- Academic Information -->
        <div class="col-lg-8">
            <div class="row">
                <!-- Attendance Summary -->
                <div class="col-md-6">
                    <div class="card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary">Attendance (This Month)</h6>
                        </div>
                        <div class="card-body text-center">
                            <h1 class="display-4 text-success">85%</h1>
                            <p class="text-muted">Present: 17 days • Absent: 3 days</p>
                        </div>
                    </div>
                </div>

                <!-- Fee Status -->
                <div class="col-md-6">
                    <div class="card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary">Fee Status</h6>
                        </div>
                        <div class="card-body text-center">
                            <h1 class="display-4 text-{{ student.fee_status_color }}">Paid</h1>
                            <p class="text-muted">Last Payment: 15 Nov 2024</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Activity -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">Recent Activity</h6>
                    <button class="btn btn-sm btn-primary">View All</button>
                </div>
                <div class="card-body">
                    <div class="timeline">
                        <!-- Add timeline items here -->
                        <p class="text-muted text-center py-4">No recent activity</p>
                    </div>
                </div>
            </div>

            <!-- Parent/Guardian Information -->
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Parent/Guardian Information</h6>
                </div>
                <div class="card-body">
                    {% if student.parentguardian_set.all %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Relationship</th>
                                    <th>Phone</th>
                                    <th>Email</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for parent in student.parentguardian_set.all %}
                                <tr>
                                    <td>{{ parent.full_name }}</td>
                                    <td>{{ parent.relationship }}</td>
                                    <td>{{ parent.phone }}</td>
                                    <td>{{ parent.email|default:"-" }}</td>
                                    <td>
                                        <a href="/admin/students/parentguardian/{{ parent.pk }}/change/" 
                                           class="btn btn-sm btn-warning">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <p class="text-muted text-center py-4">No parent/guardian information</p>
                    <div class="text-center">
                        <a href="/admin/students/parentguardian/add/?student={{ student.pk }}" 
                           class="btn btn-primary">
                            <i class="fas fa-user-plus"></i> Add Parent/Guardian
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
'''
    
    student_detail_path = os.path.join(base_dir, 'templates', 'students', 'student_detail.html')
    with open(student_detail_path, 'w', encoding='utf-8') as f:
        f.write(student_detail_content)
    print(f"✅ Created: templates/students/student_detail.html")
    
    print("\n🎉 Student portal templates created!")
    print("\n🔗 URLs to test:")
    print("   http://127.0.0.1:8000/students/  - Student List")
    print("   http://127.0.0.1:8000/students/1/ - Student Detail (replace 1 with actual ID)")

if __name__ == "__main__":
    setup_next_features()
    create_student_portal()