"""Machine Statuses API endpoints - Full CRUD."""

from flask import Blueprint, request
from flask_jwt_extended import jwt_required

from shopdb.extensions import db
from shopdb.core.models import MachineStatus
from shopdb.utils.responses import (
    success_response,
    error_response,
    paginated_response,
    ErrorCodes
)
from shopdb.utils.pagination import get_pagination_params, paginate_query

statuses_bp = Blueprint('statuses', __name__)


@statuses_bp.route('', methods=['GET'])
@jwt_required(optional=True)
def list_statuses():
    """List all machine statuses."""
    page, per_page = get_pagination_params(request)

    query = MachineStatus.query

    if request.args.get('active', 'true').lower() != 'false':
        query = query.filter(MachineStatus.isactive == True)

    query = query.order_by(MachineStatus.status)

    items, total = paginate_query(query, page, per_page)
    data = [s.to_dict() for s in items]

    return paginated_response(data, page, per_page, total)


@statuses_bp.route('/<int:status_id>', methods=['GET'])
@jwt_required(optional=True)
def get_status(status_id: int):
    """Get a single status."""
    s = MachineStatus.query.get(status_id)

    if not s:
        return error_response(
            ErrorCodes.NOT_FOUND,
            f'Status with ID {status_id} not found',
            http_code=404
        )

    return success_response(s.to_dict())


@statuses_bp.route('', methods=['POST'])
@jwt_required()
def create_status():
    """Create a new status."""
    data = request.get_json()

    if not data or not data.get('status'):
        return error_response(ErrorCodes.VALIDATION_ERROR, 'status is required')

    if MachineStatus.query.filter_by(status=data['status']).first():
        return error_response(
            ErrorCodes.CONFLICT,
            f"Status '{data['status']}' already exists",
            http_code=409
        )

    s = MachineStatus(
        status=data['status'],
        description=data.get('description'),
        color=data.get('color')
    )

    db.session.add(s)
    db.session.commit()

    return success_response(s.to_dict(), message='Status created', http_code=201)


@statuses_bp.route('/<int:status_id>', methods=['PUT'])
@jwt_required()
def update_status(status_id: int):
    """Update a status."""
    s = MachineStatus.query.get(status_id)

    if not s:
        return error_response(
            ErrorCodes.NOT_FOUND,
            f'Status with ID {status_id} not found',
            http_code=404
        )

    data = request.get_json()
    if not data:
        return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')

    if 'status' in data and data['status'] != s.status:
        if MachineStatus.query.filter_by(status=data['status']).first():
            return error_response(
                ErrorCodes.CONFLICT,
                f"Status '{data['status']}' already exists",
                http_code=409
            )

    for key in ['status', 'description', 'color', 'isactive']:
        if key in data:
            setattr(s, key, data[key])

    db.session.commit()
    return success_response(s.to_dict(), message='Status updated')


@statuses_bp.route('/<int:status_id>', methods=['DELETE'])
@jwt_required()
def delete_status(status_id: int):
    """Delete (deactivate) a status."""
    s = MachineStatus.query.get(status_id)

    if not s:
        return error_response(
            ErrorCodes.NOT_FOUND,
            f'Status with ID {status_id} not found',
            http_code=404
        )

    from shopdb.core.models import Machine
    if Machine.query.filter_by(statusid=status_id, isactive=True).first():
        return error_response(
            ErrorCodes.CONFLICT,
            'Cannot delete status: machines are using it',
            http_code=409
        )

    s.isactive = False
    db.session.commit()

    return success_response(message='Status deleted')
