<?php
session_start();
require_once 'db.php';

header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$action = $_GET['action'] ?? '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($action === 'receive_goods') {
        $data = json_decode(file_get_contents("php://input"), true);
        if (!$data || empty($data['items'])) {
            echo json_encode(['success' => false, 'message' => 'Invalid data']);
            exit;
        }

        $supplier_id = $data['supplier_id'] ?? null;
        $total_amount = $data['total_amount'] ?? 0;
        
        try {
            $pdo->beginTransaction();
            // Status defaults to 'Pending' in DB
            $stmt = $pdo->prepare("INSERT INTO purchases (supplier_id, total_amount, user_id, status) VALUES (?, ?, ?, 'Pending')");
            $stmt->execute([$supplier_id, $total_amount, $_SESSION['user_id']]);
            $purchase_id = $pdo->lastInsertId();

            $itemStmt = $pdo->prepare("INSERT INTO purchase_items (purchase_id, product_id, quantity, cost_price, subtotal) VALUES (?, ?, ?, ?, ?)");

            foreach ($data['items'] as $item) {
                $subtotal = $item['quantity'] * $item['cost_price'];
                $itemStmt->execute([$purchase_id, $item['product_id'], $item['quantity'], $item['cost_price'], $subtotal]);
                // NO stock update here anymore. Stock updates happen on Reception.
            }
            $pdo->commit();
            echo json_encode(['success' => true, 'message' => 'Purchase request created successfully. Stock will be updated upon reception.']);
        } catch (Exception $e) {
            $pdo->rollBack();
            echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
        }

    } elseif ($action === 'adjust_stock') {
        $product_id = $_POST['product_id'] ?? null;
        $type = $_POST['adjustment_type'] ?? '';
        $qty = floatval($_POST['quantity'] ?? 0);
        $reason = $_POST['reason'] ?? '';
        $batch_number = $_POST['batch_number'] ?? null;
        $expiry_date = $_POST['expiry_date'] ?? null;

        if (!$product_id || !$type || !$qty) {
            echo json_encode(['success' => false, 'message' => 'Missing required fields']);
            exit;
        }

        try {
            $pdo->beginTransaction();

            // Check if product is batch tracked
            $pStmt = $pdo->prepare("SELECT track_batches, selling_price, cost_price FROM products WHERE id = ?");
            $pStmt->execute([$product_id]);
            $product = $pStmt->fetch();

            if (!$product) throw new Exception("Product not found");

            $is_tracked = ($product['track_batches'] == 1);

            $stmt = $pdo->prepare("INSERT INTO stock_adjustments (product_id, adjustment_type, quantity, reason, user_id) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$product_id, $type, $qty, $reason, $_SESSION['user_id']]);

            if ($type === 'In') {
                $pdo->prepare("UPDATE products SET stock_quantity = stock_quantity + ? WHERE id = ?")->execute([$qty, $product_id]);
                
                if ($is_tracked && $batch_number) {
                    // Create or update batch
                    $bStmt = $pdo->prepare("SELECT id FROM product_batches WHERE product_id = ? AND batch_number = ?");
                    $bStmt->execute([$product_id, $batch_number]);
                    $batch = $bStmt->fetch();

                    if ($batch) {
                        $pdo->prepare("UPDATE product_batches SET stock_quantity = stock_quantity + ? WHERE id = ?")
                            ->execute([$qty, $batch['id']]);
                    } else {
                        $pdo->prepare("INSERT INTO product_batches (product_id, batch_number, expiry_date, stock_quantity, cost_price, selling_price) VALUES (?, ?, ?, ?, ?, ?)")
                            ->execute([$product_id, $batch_number, $expiry_date, $qty, $product['cost_price'], $product['selling_price']]);
                    }
                }
            } elseif (in_array($type, ['Out', 'Damage', 'Loss'])) {
                $pdo->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?")->execute([$qty, $product_id]);
                
                if ($is_tracked) {
                    if ($batch_number) {
                        // Deduct from specific batch
                        $pdo->prepare("UPDATE product_batches SET stock_quantity = stock_quantity - ? WHERE product_id = ? AND batch_number = ?")
                            ->execute([$qty, $product_id, $batch_number]);
                    } else {
                        // FEFO deduction
                        $remaining = $qty;
                        $batches = $pdo->prepare("SELECT id, stock_quantity FROM product_batches WHERE product_id = ? AND stock_quantity > 0 ORDER BY expiry_date ASC");
                        $batches->execute([$product_id]);
                        
                        while ($remaining > 0 && ($b = $batches->fetch())) {
                            $to_deduct = min($remaining, $b['stock_quantity']);
                            $pdo->prepare("UPDATE product_batches SET stock_quantity = stock_quantity - ? WHERE id = ?")
                                ->execute([$to_deduct, $b['id']]);
                            $remaining -= $to_deduct;
                        }
                    }
                }
            } elseif ($type === 'Set Stock') {
                $pdo->prepare("UPDATE products SET stock_quantity = ? WHERE id = ?")->execute([$qty, $product_id]);
                // Set stock is tricky with batches, we might want to allow clearing/setting but it's risky
                // For now, only update product table for Set Stock or we'd need to know which batch to set
            }

            $pdo->commit();
            echo json_encode(['success' => true, 'message' => 'Stock adjusted successfully']);
        } catch (Exception $e) {
            $pdo->rollBack();
            echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
        }
    }

} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if ($action === 'movements') {
        $start_date = $_GET['start_date'] ?? '';
        $end_date = $_GET['end_date'] ?? '';

        $sql = "SELECT * FROM (
            (SELECT r.received_at as date, 'Reception' as type, CONCAT('GRN-', r.id, ' (PO-', p.id, ') - ', s.name) as identifier, ri.quantity_received as qty, pr.name as product_name, u.username
             FROM receptions r
             JOIN purchases p ON r.purchase_id = p.id
             JOIN suppliers s ON p.supplier_id = s.id
             JOIN reception_items ri ON r.id = ri.reception_id
             JOIN products pr ON ri.product_id = pr.id
             JOIN users u ON r.received_by = u.id)
            UNION ALL
            (SELECT r.received_at as date, 'Reversal' as type, CONCAT('REVERSED PO-', p.id, ' (GRN-', r.id, ') - ', s.name) as identifier, -ri.quantity_received as qty, pr.name as product_name, u.username
             FROM receptions r
             JOIN purchases p ON r.purchase_id = p.id
             JOIN suppliers s ON p.supplier_id = s.id
             JOIN reception_items ri ON r.id = ri.reception_id
             JOIN products pr ON ri.product_id = pr.id
             JOIN users u ON r.received_by = u.id
             WHERE p.status = 'Cancelled')
            UNION ALL
            (SELECT sale_date as date, 'Sale' as type, CONCAT('Receipt #', s.id) as identifier, -si.quantity as qty, pr.name as product_name, u.username
             FROM sales s
             JOIN sale_items si ON s.id = si.sale_id
             JOIN products pr ON si.product_id = pr.id
             JOIN users u ON s.user_id = u.id)
            UNION ALL
            (SELECT adjustment_date as date, adjustment_type as type, reason as identifier, 
                CASE 
                    WHEN adjustment_type IN ('Out', 'Damage', 'Loss') THEN -quantity 
                    ELSE quantity 
                END as qty, 
                pr.name as product_name, u.username
             FROM stock_adjustments sa
             JOIN products pr ON sa.product_id = pr.id
             JOIN users u ON sa.user_id = u.id)
        ) as movements WHERE 1=1";

        $params = [];
        if ($start_date) {
            $sql .= " AND DATE(date) >= ?";
            $params[] = $start_date;
        }
        if ($end_date) {
            $sql .= " AND DATE(date) <= ?";
            $params[] = $end_date;
        }

        $sql .= " ORDER BY date DESC LIMIT 1000";
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
        echo json_encode(['success' => true, 'data' => $stmt->fetchAll()]);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Unknown action']);
}
