const express = require('express');
const router = express.Router();
const Place = require('../models/Place');
const PlaceCategory = require('../models/PlaceCategory');
const User = require('../models/User');
const { protect } = require('../middleware/authMiddleware');
const logger = require('../utils/logger');

// ============================================================
// @route   GET /api/places/categories
// @desc    Get all active categories
// ============================================================
router.get('/categories', async (req, res) => {
    try {
        const categories = await PlaceCategory.find({ isActive: true }).sort({ sortOrder: 1, name: 1 });
        res.json(categories);
    } catch (err) {
        logger.error('Places Categories Error:', err);
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   GET /api/places?category_id=X
// @desc    Get places for a category
// ============================================================
router.get('/', async (req, res) => {
    try {
        const { category_id } = req.query;
        const query = { isActive: true };
        if (category_id) query.category = category_id;

        const places = await Place.find(query).populate('category', 'name icon');
        // Return with virtuals + ownerId so client can show "Browse Products" button for merchant stores
        res.json(places.map(p => {
            const obj = p.toJSON();
            // Explicitly include ownerId so the client knows if this place has a merchant
            if (p.ownerId) obj.ownerId = p.ownerId;
            return obj;
        }));
    } catch (err) {
        logger.error('Places List Error:', err);
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   GET /api/places/:id
// @desc    Get a single place by ID
// ============================================================
router.get('/:id', async (req, res) => {
    try {
        const place = await Place.findById(req.params.id).populate('category', 'name icon');
        if (!place) return res.status(404).json({ message: 'المحل غير موجود' });
        res.json(place.toJSON());
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   POST /api/places/categories
// @desc    Admin: Create a category
// ============================================================
router.post('/categories', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });
        const { name, icon, sortOrder, notes } = req.body;
        if (!name) return res.status(400).json({ message: 'اسم الفئة مطلوب' });
        const cat = await PlaceCategory.create({ name, icon, sortOrder, notes });
        res.status(201).json(cat);
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// @route   PUT /api/places/categories/:id
router.put('/categories/:id', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });
        const cat = await PlaceCategory.findByIdAndUpdate(req.params.id, req.body, { new: true });
        if (!cat) return res.status(404).json({ message: 'غير موجود' });
        res.json(cat);
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// @route   DELETE /api/places/categories/:id
router.delete('/categories/:id', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });
        await PlaceCategory.findByIdAndUpdate(req.params.id, { isActive: false });
        res.json({ message: 'تم إخفاء التصنيف' });
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   POST /api/places
// @desc    Admin: Create a place (optionally with a new merchant account)
// ============================================================
router.post('/', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });

        const {
            name, category, image_url, phone, whatsapp, location, address,
            map_url, workingHours, menu, notes,
            // 👤 بيانات التاجر (اختياري)
            ownerName, ownerPhone, ownerEmail, ownerPassword, ownerBankAccount,
            ownerId // ربط بمستخدم موجود مسبقاً
        } = req.body;

        if (!name || !category || !location) {
            return res.status(400).json({ message: 'الاسم والفئة والموقع مطلوبة' });
        }

        let resolvedOwnerId = ownerId || null;

        // ─── حالة 1: إنشاء حساب تاجر جديد ───────────────────────
        if (ownerName && ownerPhone && ownerPassword && !ownerId) {
            // تحقق من عدم تكرار الهاتف
            const existingUser = await User.findOne({ phone: ownerPhone });
            if (existingUser) {
                return res.status(400).json({ message: `رقم الهاتف ${ownerPhone} مسجّل مسبقاً لمستخدم آخر` });
            }

            const newMerchant = new User({
                name: ownerName,
                phone: ownerPhone,
                email: ownerEmail || undefined,
                password: ownerPassword,
                role: 'merchant',
                approvalStatus: 'approved',
                isVerified: true
            });
            await newMerchant.save();
            resolvedOwnerId = newMerchant._id;

            // حفظ رقم الحساب البنكي في MerchantRequest إن أُرسل
            if (ownerBankAccount) {
                const MerchantRequest = require('../models/MerchantRequest');
                const PlaceCategory = require('../models/PlaceCategory');
                let categoryDoc = null;
                if (require('mongoose').Types.ObjectId.isValid(category)) {
                    categoryDoc = await PlaceCategory.findById(category);
                }
                await MerchantRequest.create({
                    userId: resolvedOwnerId,
                    businessName: name,
                    ownerName,
                    phone: ownerPhone,
                    category: categoryDoc ? categoryDoc.name : '',
                    address: address || '',
                    bankAccount: ownerBankAccount,
                    status: 'approved',
                    logoImage: image_url || ''
                });
            }
        }

        // ─── حالة 2: ربط بمستخدم موجود ──────────────────────────
        if (ownerId && !ownerName) {
            // تأكد إن المستخدم موجود وحوّل دوره لتاجر
            await User.findByIdAndUpdate(ownerId, { role: 'merchant', approvalStatus: 'approved' });
        }

        // ─── إنشاء المتجر ─────────────────────────────────────────
        const place = await Place.create({
            name, category, image_url, phone, whatsapp, location, address,
            map_url, workingHours, menu, notes,
            ownerId: resolvedOwnerId,
            isActive: true,
            isOpenOverride: true,
            deliveryAvailable: true
        });

        res.status(201).json(place.toJSON());
    } catch (err) {
        logger.error('Create Place Error:', err);
        res.status(500).json({ message: err.message || 'Server Error' });
    }
});

// ============================================================
// @route   PUT /api/places/:id
// @desc    Admin: Update a place
// ============================================================
router.put('/:id', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });
        const place = await Place.findByIdAndUpdate(req.params.id, req.body, { new: true });
        if (!place) return res.status(404).json({ message: 'غير موجود' });
        res.json(place.toJSON());
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   DELETE /api/places/:id
// @desc    Admin: Delete/deactivate a place
// ============================================================
router.delete('/:id', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });
        
        const Place = require('../models/Place');
        const place = await Place.findById(req.params.id);
        if (!place) return res.status(404).json({ message: 'المتجر غير موجود' });

        const ownerId = place.ownerId;

        // 1. Delete the Place and its Products
        await Place.findByIdAndDelete(req.params.id);
        
        const Product = require('../models/Product');
        await Product.deleteMany({ placeId: req.params.id });

        if (ownerId) {
            // 2. Delete the associated MerchantRequest so it disappears from the requests page
            const MerchantRequest = require('../models/MerchantRequest');
            await MerchantRequest.findOneAndDelete({ userId: ownerId });

            // 3. Check if user has other places, if not, revert role to client
            const otherPlaces = await Place.countDocuments({ ownerId: ownerId });
            if (otherPlaces === 0) {
                const User = require('../models/User');
                await User.findByIdAndUpdate(ownerId, { role: 'client' });
            }
        }

        res.json({ message: 'تم حذف المتجر وبياناته ومزامنته بنجاح' });
    } catch (err) {
        res.status(500).json({ message: 'Server Error' });
    }
});

// ============================================================
// @route   POST /api/places/seed-demo
// @desc    Admin: Seed demo categories & places for testing
// ============================================================
router.post('/seed-demo', protect, async (req, res) => {
    try {
        if (req.user.role !== 'admin') return res.status(403).json({ message: 'Admins only' });

        // Clear existing
        await PlaceCategory.deleteMany({});
        await Place.deleteMany({});

        const categories = await PlaceCategory.insertMany([
            { name: 'صيدليات', icon: 'bi-capsule-pill', sortOrder: 1 },
            { name: 'مطاعم', icon: 'bi-cup-hot-fill', sortOrder: 2 },
            { name: 'سوبرماركت', icon: 'bi-cart-fill', sortOrder: 3 },
            { name: 'مخابز', icon: 'bi-egg-fried', sortOrder: 4 },
            { name: 'إلكترونيات', icon: 'bi-phone-fill', sortOrder: 5 },
            { name: 'ملابس', icon: 'bi-bag-fill', sortOrder: 6 },
        ]);

        const pharma = categories[0]._id;
        const restaurants = categories[1]._id;
        const supermarket = categories[2]._id;
        const bakery = categories[3]._id;

        // Demo places around Khartoum
        await Place.insertMany([
            {
                name: 'صيدلية النيل', category: pharma,
                image_url: 'https://images.unsplash.com/photo-1631549916768-4119b2e5f926?w=400',
                phone: '0912345678', whatsapp: '249912345678',
                location: { lat: 15.5010, lng: 32.5590 },
                workingHours: { open: '08:00', close: '22:00', days: [0, 1, 2, 3, 4, 5, 6] }
            },
            {
                name: 'صيدلية الخرطوم', category: pharma,
                image_url: 'https://images.unsplash.com/photo-1582281298055-e25b84a30b0b?w=400',
                phone: '0922345678', whatsapp: '249922345678',
                location: { lat: 15.5050, lng: 32.5630 },
                workingHours: { open: '09:00', close: '21:00', days: [1, 2, 3, 4, 5, 6] }
            },
            {
                name: 'مطعم البيت السوداني', category: restaurants,
                image_url: 'https://images.unsplash.com/photo-1567620905732-2d1ec7ab7445?w=400',
                phone: '0933345678', whatsapp: '249933345678',
                location: { lat: 15.4980, lng: 32.5570 },
                workingHours: { open: '07:00', close: '23:00', days: [0, 1, 2, 3, 4, 5, 6] }
            },
            {
                name: 'مطعم شاورما بيروت', category: restaurants,
                image_url: 'https://images.unsplash.com/photo-1529006557810-274b9b2fc783?w=400',
                phone: '0944345678', whatsapp: '249944345678',
                location: { lat: 15.5035, lng: 32.5615 },
                workingHours: { open: '11:00', close: '02:00', days: [0, 1, 2, 3, 4, 5, 6] }
            },
            {
                name: 'سوبرماركت الفردوس', category: supermarket,
                image_url: 'https://images.unsplash.com/photo-1604719312566-8912e9227c6a?w=400',
                phone: '0955345678', whatsapp: '249955345678',
                location: { lat: 15.5020, lng: 32.5600 },
                workingHours: { open: '07:00', close: '23:00', days: [0, 1, 2, 3, 4, 5, 6] }
            },
            {
                name: 'مخبز الأمل', category: bakery,
                image_url: 'https://images.unsplash.com/photo-1509440159596-0249088772ff?w=400',
                phone: '0966345678', whatsapp: '249966345678',
                location: { lat: 15.4995, lng: 32.5580 },
                workingHours: { open: '05:00', close: '20:00', days: [0, 1, 2, 3, 4, 5, 6] }
            }
        ]);

        res.json({
            message: 'تم إدراج البيانات التجريبية بنجاح ✅',
            categories: categories.length,
            places: 6
        });

    } catch (err) {
        logger.error('Seed Error:', err);
        res.status(500).json({ message: err.message });
    }
});

module.exports = router;
