from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from django.db.models import Q, Sum, Count, F
from django.db.models.functions import TruncMonth
from django.urls import reverse
from inventory.models import Product
from customers.models import Customer
from suppliers.models import Supplier
from sales.models import Sale
from hr.models import Employee, Attendance
from orders.models import Order
from django.contrib.auth.decorators import login_required
from django.utils import timezone
from datetime import timedelta
import csv


@login_required
def global_search(request):
    query = request.GET.get('q', '').strip()

    if len(query) < 2:
        return JsonResponse({'results': []})

    results = []

    # Search Products
    try:
        products = Product.objects.filter(
            Q(name__icontains=query) |
            Q(barcode__icontains=query) |
            Q(description__icontains=query),
            is_active=True
        )[:5]

        for product in products:
            results.append({
                'category': 'Product',
                'title': product.name,
                'subtitle': f'Price: KSh {product.selling_price} | Stock: {product.stock_quantity}',
                'url': reverse('product_detail', args=[product.id])
            })
    except:
        pass

    # Search Customers
    try:
        customers = Customer.objects.filter(
            Q(first_name__icontains=query) |
            Q(last_name__icontains=query) |
            Q(phone__icontains=query) |
            Q(email__icontains=query) |
            Q(customer_id__icontains=query)
        )[:5]

        for customer in customers:
            results.append({
                'category': 'Customer',
                'title': f'{customer.first_name} {customer.last_name}',
                'subtitle': f'ID: {customer.customer_id} | Phone: {customer.phone}',
                'url': reverse('customer_detail', args=[customer.customer_id])
            })
    except:
        pass

    # Search Suppliers
    try:
        suppliers = Supplier.objects.filter(
            Q(name__icontains=query) |
            Q(contact_person__icontains=query) |
            Q(phone__icontains=query) |
            Q(email__icontains=query) |
            Q(supplier_id__icontains=query)
        )[:5]

        for supplier in suppliers:
            results.append({
                'category': 'Supplier',
                'title': supplier.name,
                'subtitle': f'ID: {supplier.supplier_id} | Contact: {supplier.contact_person or "N/A"}',
                'url': reverse('view_supplier', args=[supplier.id])
            })
    except:
        pass

    # Search Sales
    try:
        sales = Sale.objects.filter(
            Q(id__icontains=query) |
            Q(customer__first_name__icontains=query) |
            Q(customer__last_name__icontains=query)
        )[:3]

        for sale in sales:
            customer_name = f'{sale.customer.first_name} {sale.customer.last_name}' if sale.customer else 'Walk-in Customer'
            results.append({
                'category': 'Sale',
                'title': f'Sale #{sale.id}',
                'subtitle': f'Customer: {customer_name} | Amount: KSh {sale.total_amount}',
                'url': reverse('sale_detail', args=[sale.id])
            })
    except:
        pass

    # Search Employees
    try:
        employees = Employee.objects.filter(
            Q(first_name__icontains=query) |
            Q(last_name__icontains=query) |
            Q(employee_id__icontains=query) |
            Q(job_title__icontains=query)
        )[:3]

        for employee in employees:
            results.append({
                'category': 'Employee',
                'title': f'{employee.first_name} {employee.last_name}',
                'subtitle': f'ID: {employee.employee_id} | Position: {employee.job_title}',
                'url': reverse('employee_detail', args=[employee.id])
            })
    except:
        pass

    # Search Orders
    try:
        orders = Order.objects.filter(
            Q(order_number__icontains=query) |
            Q(supplier__name__icontains=query)
        )[:3]

        for order in orders:
            results.append({
                'category': 'Order',
                'title': f'Order {order.order_number}',
                'subtitle': f'Supplier: {order.supplier.name} | Total: KSh {order.total_cost}',
                'url': reverse('order_detail', args=[order.id])
            })
    except:
        pass

    return JsonResponse({'results': results})


@login_required(login_url='/auth/login/')
def dashboard_view(request):
    # Get the current date
    today = timezone.now().date()
    expiry_threshold = today + timedelta(days=30)

    # Calculate time periods
    start_of_week = today - timedelta(days=today.weekday())
    start_of_month = today.replace(day=1)
    six_months_ago = today - timedelta(days=6*30)
    two_weeks_ago = today - timedelta(days=14)

    # Sales Overview - Use the same comprehensive approach as sales dashboard
    daily_sales = Sale.objects.filter(sale_date__date=today).aggregate(
        total_sales=Sum('total_amount'),
        cash_sales=Sum('total_amount', filter=Q(payment_method='cash')),
        mpesa_sales=Sum('total_amount', filter=Q(payment_method='mpesa')),
        credit_sales=Sum('total_amount', filter=Q(payment_method='credit')),
        cheque_sales=Sum('total_amount', filter=Q(payment_method='cheque')),
        transaction_count=Count('id')
    )

    weekly_sales = Sale.objects.filter(sale_date__date__gte=start_of_week).aggregate(
        total_sales=Sum('total_amount'),
        cash_sales=Sum('total_amount', filter=Q(payment_method='cash')),
        mpesa_sales=Sum('total_amount', filter=Q(payment_method='mpesa')),
        credit_sales=Sum('total_amount', filter=Q(payment_method='credit')),
        cheque_sales=Sum('total_amount', filter=Q(payment_method='cheque'))
    )

    monthly_sales = Sale.objects.filter(sale_date__date__gte=start_of_month).aggregate(
        total_sales=Sum('total_amount'),
        cash_sales=Sum('total_amount', filter=Q(payment_method='cash')),
        mpesa_sales=Sum('total_amount', filter=Q(payment_method='mpesa')),
        credit_sales=Sum('total_amount', filter=Q(payment_method='credit')),
        cheque_sales=Sum('total_amount', filter=Q(payment_method='cheque'))
    )

    # Overall totals
    total_sales = Sale.objects.aggregate(total=Sum('total_amount'))['total'] or 0
    
    profit_loss = total_sales - 0 # Removed total_expenses

    # Sales by payment method
    sales_by_payment_method = Sale.objects.values('payment_method').annotate(
        total=Sum('total_amount')
    ).order_by('-total')

    # Orders
    try:
        pending_orders = Order.objects.filter(is_received=False).count()
    except:
        pending_orders = 0

    # Inventory Overview
    try:
        low_stock_count = Product.objects.filter(stock_quantity__lt=F('low_stock_threshold')).count()
        near_expiry_count = Product.objects.filter(expiry_date__lte=expiry_threshold, expiry_date__gte=today).count()
        inventory_value = Product.objects.filter(stock_quantity__gt=0, buying_price__gt=0).aggregate(
            total_value=Sum(F('stock_quantity') * F('buying_price'))
        )['total_value'] or 0
    except:
        low_stock_count = 0
        near_expiry_count = 0
        inventory_value = 0

    # Customer Overview
    try:
        top_customers = Customer.objects.annotate(
            total_spent=Sum('sale__total_amount')
        ).filter(total_spent__gt=0).order_by('-total_spent')[:5]
        customer_growth = Customer.objects.filter(date_added__gte=start_of_month).count()
    except:
        top_customers = []
        customer_growth = 0

    # HR Overview
    try:
        employee_count = Employee.objects.filter(is_active=True).count()
        employee_attendance = Attendance.objects.filter(date=today)
    except:
        employee_count = 0
        employee_attendance = []

    context = {
        'total_sales': total_sales,
        'profit_loss': profit_loss, # Removed total_expenses
        'pending_orders': pending_orders,
        'low_stock_count': low_stock_count,
        'near_expiry_count': near_expiry_count,
        'inventory_value': inventory_value,
        'inventory_report_url': reverse('reports:inventory_report'),
        'daily_sales': daily_sales['total_sales'] or 0,
        'weekly_sales': weekly_sales['total_sales'] or 0,
        'monthly_sales': monthly_sales['total_sales'] or 0,
        'sales_by_payment_method': sales_by_payment_method,
        'top_customers': top_customers,
        'customer_growth': customer_growth,
        'employee_count': employee_count,
        'employee_attendance': employee_attendance,
    }

    return render(request, 'dashboard/dashboard.html', context)


# Onboarding Guide Views
@login_required(login_url='/auth/login/')
def onboarding_home(request):
    """Main onboarding guide page"""
    context = {
        'page_title': 'Getting Started with Sales Shark',
        'guides': [
            {
                'title': 'Create Your First Customer',
                'description': 'Learn how to add customers to track sales and manage customer relationships.',
                'url': 'onboarding_customers',
                'icon': 'fas fa-users',
                'difficulty': 'Easy',
                'time': '3 minutes'
            },
            {
                'title': 'Add Your First Product',
                'description': 'Set up your inventory by adding products with pricing and stock information.',
                'url': 'onboarding_products',
                'icon': 'fas fa-box',
                'difficulty': 'Easy',
                'time': '5 minutes'
            },
            {
                'title': 'Import Products from Excel',
                'description': 'Bulk import your entire product catalog using our Excel template.',
                'url': 'onboarding_import',
                'icon': 'fas fa-file-excel',
                'difficulty': 'Medium',
                'time': '10 minutes'
            },
            {
                'title': 'Add Your First Supplier',
                'description': 'Manage your supply chain by adding supplier information and contacts.',
                'url': 'onboarding_suppliers',
                'icon': 'fas fa-truck',
                'difficulty': 'Easy',
                'time': '4 minutes'
            },
            {
                'title': 'Register Your First Employee',
                'description': 'Set up employee records for HR management and access control.',
                'url': 'onboarding_employees',
                'icon': 'fas fa-user-tie',
                'difficulty': 'Medium',
                'time': '6 minutes'
            }
        ]
    }
    return render(request, 'dashboard/onboarding_home.html', context)


@login_required(login_url='/auth/login/')
def onboarding_customers(request):
    """Customer creation guide"""
    context = {
        'page_title': 'Create Your First Customer',
        'current_step': 1,
        'total_steps': 4,
        'add_customer_url': reverse('customer_add'),
        'customer_list_url': reverse('customer_list'),
    }
    return render(request, 'dashboard/onboarding_customers.html', context)


@login_required(login_url='/auth/login/')
def onboarding_products(request):
    """Product creation guide"""
    context = {
        'page_title': 'Add Your First Product',
        'current_step': 2,
        'total_steps': 4,
        'add_product_url': reverse('add_product'),
        'product_list_url': reverse('product_list'),
    }
    return render(request, 'dashboard/onboarding_products.html', context)


@login_required(login_url='/auth/login/')
def onboarding_import(request):
    """Product import guide"""
    context = {
        'page_title': 'Import Products from Excel',
        'current_step': 3,
        'total_steps': 4,
        'import_url': reverse('import_products'),
        'sample_csv_url': reverse('download_sample_csv'),
    }
    return render(request, 'dashboard/onboarding_import.html', context)


@login_required(login_url='/auth/login/')
def onboarding_suppliers(request):
    """Supplier creation guide"""
    context = {
        'page_title': 'Add Your First Supplier',
        'current_step': 4,
        'total_steps': 4,
        'add_supplier_url': reverse('add_supplier'),
        'supplier_list_url': reverse('supplier_list'),
    }
    return render(request, 'dashboard/onboarding_suppliers.html', context)


@login_required(login_url='/auth/login/')
def onboarding_employees(request):
    """Employee creation guide"""
    context = {
        'page_title': 'Register Your First Employee',
        'current_step': 5,
        'total_steps': 5,
        'add_employee_url': reverse('employee_add'),
        'employee_list_url': reverse('employee_list'),
    }
    return render(request, 'dashboard/onboarding_employees.html', context)


@login_required(login_url='/auth/login/')
def download_sample_csv(request):
    """Generate and download sample CSV file for product import"""
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="sample_products.csv"'
    
    writer = csv.writer(response)
    # Header row
    writer.writerow(['barcode', 'name', 'selling_price', 'stock_quantity', 'buying_price', 'expiry_date', 'packaging_type', 'category'])
    
    # Sample data rows
    writer.writerow(['001234567890', 'Premium Coffee Beans 500g', '1200.00', '25', '800.00', '2025-12-31', 'Packets', 'Beverages'])
    writer.writerow(['001234567891', 'Organic Rice 2kg', '450.00', '50', '320.00', '2025-06-30', 'Bags', 'Grains'])
    writer.writerow(['001234567892', 'Natural Honey 250ml', '650.00', '15', '400.00', '2026-03-15', 'Bottles', 'Sweeteners'])
    writer.writerow(['001234567893', 'Fresh Milk 1L', '120.00', '40', '85.00', '2025-01-15', 'Cartons', 'Dairy'])
    writer.writerow(['001234567894', 'Whole Wheat Bread', '80.00', '30', '60.00', '2025-01-05', 'Loaves', 'Bakery'])
    
    return response