from flask import Blueprint, request, jsonify
from flask_login import current_user, login_required

from app.services.regions_service import RegionService

region_bp = Blueprint('region', __name__,url_prefix='/regions')
region = RegionService()

@region_bp.route('/', methods=['GET'], strict_slashes=False)
@login_required
def list_regions():
    return jsonify(region.get_regions(
        page=request.args.get('page', 1, type=int),
        per_page=request.args.get('per_page', 10, type=int),
        sort_field=request.args.get('sort_field', 'region_id'),
        sort_order=request.args.get('sort_order', 'asc')
    ))

@region_bp.route('/', methods=['POST'], strict_slashes=False)
@login_required
def add_region():
    return region.add_region(request.json, current_user)

@region_bp.route('/<int:region_id>', methods=['DELETE'])
@login_required
def delete_region(region_id):
    return region.delete_region(region_id, current_user)

@region_bp.route('/name', methods=['PUT'])
@login_required
def update_name():
    return region.update_region_name(request.json, current_user)

@region_bp.route('/status', methods=['PUT'])
@login_required
def update_status():
    return region.update_region_status(request.json, current_user)

@region_bp.route('/<int:region_id>/subscribers', methods=['GET'])
@login_required
def region_subscribers(region_id):
    return region.get_region_subscribers(region_id)