from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from core.models import Plant, Category, Product, Inventory, Machine, WorkOrder, Inspection
import random
from datetime import date, timedelta

class Command(BaseCommand):
    help = 'Seeds the database with initial demo data for the ERP'

    def handle(self, *args, **kwargs):
        self.stdout.write('Seeding data...')

        # Ensure we have an admin user
        if not User.objects.filter(username='admin').exists():
            User.objects.create_superuser('admin', 'admin@example.com', 'admin')
            self.stdout.write('Superuser "admin" created.')

        # Plants
        p1, _ = Plant.objects.get_or_create(name='Istanbul Facility', location='Istanbul, TR')
        p2, _ = Plant.objects.get_or_create(name='Ankara Facility', location='Ankara, TR')
        p3, _ = Plant.objects.get_or_create(name='Izmir Facility', location='Izmir, TR')

        # Categories
        cat_raw, _ = Category.objects.get_or_create(name='Raw Materials')
        cat_final, _ = Category.objects.get_or_create(name='Finished Goods')
        cat_comp, _ = Category.objects.get_or_create(name='Components')

        # Products
        prod1, _ = Product.objects.get_or_create(name='Steel Plate', sku='RAW-001', category=cat_raw)
        prod2, _ = Product.objects.get_or_create(name='Microchip A1', sku='COMP-101', category=cat_comp)
        prod3, _ = Product.objects.get_or_create(name='Smart Widget', sku='FIN-505', category=cat_final)

        # Inventory
        for p in [p1, p2, p3]:
            for prod in [prod1, prod2, prod3]:
                Inventory.objects.get_or_create(
                    plant=p,
                    product=prod,
                    lot_number=f"LOT-{random.randint(1000, 9999)}",
                    defaults={'quantity': random.uniform(10, 500)}
                )

        # Machines
        m1, _ = Machine.objects.get_or_create(name='Laser Cutter X1', plant=p1, status='active')
        m2, _ = Machine.objects.get_or_create(name='Assembly Line 3', plant=p2, status='active')
        m3, _ = Machine.objects.get_or_create(name='Quality Tester T8', plant=p3, status='maintenance')

        # Work Orders
        wo1, _ = WorkOrder.objects.get_or_create(
            order_number='WO-2026-001',
            plant=p1,
            product=prod3,
            quantity=100,
            machine=m1,
            status='in_progress'
        )
        wo2, _ = WorkOrder.objects.get_or_create(
            order_number='WO-2026-002',
            plant=p2,
            product=prod3,
            quantity=50,
            machine=m2,
            status='planned'
        )

        # Inspections
        admin_user = User.objects.get(username='admin')
        Inspection.objects.get_or_create(
            work_order=wo1,
            inspector=admin_user,
            result='pass',
            notes='All systems nominal.'
        )

        self.stdout.write(self.style.SUCCESS('Successfully seeded demo data.'))
