const {
  createZone,
  getAllZones,
  getZoneById,
  getZonesByCity,
  updateZone,
  deleteZone,
  updateZoneAdmin,
  getZoneStatistics,
  getZoneAnalytics,
  changeZoneStatus,
  getZonesOverview,
  getOrdersByStatusForZone
} = require('../controllers/zone.controller');

const { allowRoles } = require('../middlewares/authenticate.js');
const { checkPermission } = require('../middlewares/checkPermission.js');

const router = require('express').Router();

// POST - Create Zone
router.post(
  '/create-zone',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Create a new operating zone and dedicated zone admin (super-admin only)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
  schema: {
  $zoneName: "Indrapuri",
  zoneAddress: {
    address: "Street 12, Indrapuri",
    city: "Bhopal",
    state: "Madhya Pradesh"
  },
   extraFareRule: {
    fareType: "flat",
    fareAmount: 10,
    reason: "Night tariff"
  },
  $zoneContact: "+91-9876543210",
  $coordinates: [
    {
      lat: 23.254430,
      lng: 77.456662
    },
    {
      lat: 23.253414,
      lng: 77.467430
    },
    {
      lat: 23.250105,
      lng: 77.466984
    },
    {
      lat: 23.251845,
      lng: 77.456404
    }
  ],
 
  $zoneAdmin: {
    $name: "jon deo",
    $email: "jon@gmail.com",
    $phone: "+1555012345",
    $password: "123456"
  }
}

      }
  */
  createZone
);

// GET - All Zones (with optional filters)
router.get(
  '/',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),

  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Get all zones with optional filters (super-admin only). Without filters returns all zones, with filters returns filtered results.'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = {
        in: 'query',
        type: 'number',
        description: 'Page number (default: 1)'
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        type: 'number',
        description: 'Items per page (default: 10)'
      }
      #swagger.parameters['startDate'] = { in: 'query', type: 'string', description: 'Start date filter (YYYY-MM-DD)' }
      #swagger.parameters['endDate'] = { in: 'query', type: 'string', description: 'End date filter (YYYY-MM-DD)' }
      #swagger.parameters['zoneName'] = { in: 'query', type: 'string', description: 'Filter by zone name (partial match)' }
      #swagger.parameters['isActive'] = { in: 'query', type: 'boolean', description: 'Filter by active status' }
      #swagger.parameters['minRating'] = { in: 'query', type: 'number', description: 'Minimum rating filter' }
      #swagger.parameters['zoneAdmin'] = { in: 'query', type: 'string', description: 'Zone Admin name filter (partial match)' }
      #swagger.parameters['minFare'] = { in: 'query', type: 'number', description: 'Minimum base fare filter' }
      #swagger.parameters['maxFare'] = { in: 'query', type: 'number', description: 'Maximum base fare filter' }
      #swagger.parameters['city'] = { in: 'query', type: 'string', description: 'Filter by city name (partial match)' }
  */
  getAllZones
);

// GET - Zones Overview (for super admin dashboard)
router.get(
  '/overview',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Get zones overview statistics for dashboard (avg vendors, drivers, orders, revenue per zone)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.responses[200] = {
        description: 'Zones overview statistics',
        schema: {
          success: true,
          totalZones: 5,
          avgAreaPerZone: 18.0,
          avgVendorsPerZone: 24,
          avgDriversPerZone: 24,
          avgOrdersPerZone: 24,
          avgRevenuePerZone: 72551
        }
      }
  */
  getZonesOverview
);

// GET - Zones by City (must come before /:id to avoid route conflicts)
// router.get(
//   '/city/:city',
//   allowRoles('super-admin'),
//   /*  #swagger.tags = ['Zone']
//       #swagger.description = 'Get zones by city name (super-admin only)'
//       #swagger.security = [{ "bearerAuth": [] }]
//       #swagger.parameters['city'] = {
//         in: 'path',
//         required: true,
//         type: 'string',
//         description: 'City name'
//       }
//   */
//   getZonesByCity
// );

// GET - Zone by ID
router.get(
  '/:id',
  allowRoles('super-admin', 'zone-admin', 'staff'),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Get zone by ID (super-admin can access any zone, zone-admin can only access their assigned zone)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        description: 'Zone ID'
      }
  */
  getZoneById
);

// GET - Zone Analytics (detailed report)
router.get(
  '/:id/analytics',
  allowRoles('super-admin', 'zone-admin', 'staff'),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Get detailed zone analytics and reports'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        description: 'Zone ID'
      }
      #swagger.parameters['startDate'] = { in: 'query', type: 'string', description: 'Start date filter (YYYY-MM-DD)' }
      #swagger.parameters['endDate'] = { in: 'query', type: 'string', description: 'End date filter (YYYY-MM-DD)' }
      #swagger.responses[200] = {
        description: 'Zone analytics data',
        schema: {
          success: true,
          zone: { id: "zone_id", name: "North Zone", city: "Bhopal", isActive: true },
          analytics: {
            totalOrders: 102,
            totalRevenue: 398743,
            completedOrders: 85,
            cancelledOrders: 5,
            ordersByStatus: [
              { status: "Completed", count: 85, percentage: 83.3 },
              { status: "Cancelled", count: 5, percentage: 4.9 }
            ],
            topVendors: [
              { vendorId: "vendor_id", name: "Express Fleet", orders: 45, revenue: 125000, rating: 4.8 }
            ]
          }
        }
      }
  */
  getZoneAnalytics
);

// PATCH - Update Zone
router.patch(
  '/:id',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /*  #swagger.tags = ['Zone']
       #swagger.description = 'Create a new operating zone and dedicated zone admin (super-admin only)'
       #swagger.security = [{ "bearerAuth": [] }]
       #swagger.parameters['body'] = {
         in: 'body',
   schema: {
   $zoneName: "Indrapuri",
   zoneAddress: {
     address: "Street 12, Indrapuri",
     city: "Bhopal",
     state: "Madhya Pradesh"
   },
    extraFareRule: {
     fareType: "flat",
     fareAmount: 10,
     reason: "Night tariff"
   },
   $zoneContact: "+91-9876543210",
   $coordinates: [
     {
       lat: 23.254430,
       lng: 77.456662
     },
     {
       lat: 23.253414,
       lng: 77.467430
     },
     {
       lat: 23.250105,
       lng: 77.466984
     },
     {
       lat: 23.251845,
       lng: 77.456404
     }
   ],
  
 }
 
       }
   */
  updateZone
);

router.patch(
  "/:id/update/zone-admin",
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /* #swagger.tags = ['Zone'] 
   #swagger.description = 'Get zone by ID (super-admin only)'
   #swagger.security = [{ "bearerAuth": [] }]
     #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        description: 'Zone ID'
      }
        #swagger.parameters['body'] = {
        in: 'body' ,
        schema:{
        $name:"jon",
        $email:"jon@gmail.com",
        $phone:"1234567890"
        }

     }
   
   */
  updateZoneAdmin)

// PATCH - Update Zone Status
router.patch(
  '/:id/toggle-status',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Update zone status (super-admin only)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        description: 'Zone ID'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        schema: {
          isActive: true
        }
      }
  */
  changeZoneStatus
);
// DELETE - Delete Zone
router.delete(
  '/:id/delete-zone',
  allowRoles('super-admin', 'staff'),
  checkPermission("manage_zones"),
  /*  #swagger.tags = ['Zone']
      #swagger.description = 'Delete zone (super-admin only)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'string',
        description: 'Zone ID'
      }
  */
  deleteZone
);

router.get('/orders/analytics/:id', allowRoles('super-admin', 'zone-admin', 'staff'), checkPermission("manage_zones"),

  /*
  #swagger.tags = ['Zone'] 
  #swagger.description = 'Get zone analytics (super-admin and zone-admin only)'
  #swagger.security = [{ "bearerAuth": [] }]
  #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Zone ID'
  }
  */
  getOrdersByStatusForZone)


module.exports = router;
