#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  3 00:59:15 2025

@author: dromisch
"""

from flask import Blueprint, jsonify, request

route1 = Blueprint('route1', __name__)

# Data storage
data = [
    {"id": 1, "name": "Item 1"},
    {"id": 2, "name": "Item 2"}
]

# Get all items
@route1.route('/items', methods=['GET'])
def get_items():
    return jsonify(data)

# Get a single item by ID
@route1.route('/items/<int:item_id>', methods=['GET'])
def get_item(item_id):
    item = next((item for item in data if item["id"] == item_id), None)
    if item:
        return jsonify(item)
    return jsonify({"error": "Item not found"}), 404

# Create a new item
@route1.route('/items', methods=['POST'])
def create_item():
    new_item = request.json
    data.append(new_item)
    return jsonify(new_item), 201

# Update an item
@route1.route('/items/<int:item_id>', methods=['PUT'])
def update_item(item_id):
    item = next((item for item in data if item["id"] == item_id), None)
    if item:
        updated_data = request.json
        item.update(updated_data)
        return jsonify(item)
    return jsonify({"error": "Item not found"}), 404

# Delete an item
@route1.route('/items/<int:item_id>', methods=['DELETE'])
def delete_item(item_id):
    global data
    data = [item for item in data if item["id"] != item_id]
    return jsonify({"message": "Item deleted"}), 200
