package com.example.hotelbilling;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@RestController
@RequestMapping("/api/bills")
public class HotelBillingApplication {

    public static void main(String[] args) {
        SpringApplication.run(HotelBillingApplication.class, args);
    }

    private List<Bill> bills = new ArrayList<>();

    @GetMapping
    public List<Bill> getAllBills() {
        return bills;
    }

    @GetMapping("/api/{id}")
    public Bill getBillById(@PathVariable int id) {
        return bills.stream().filter(bill -> bill.getId() == id).findFirst().orElse(null);
    }

    @PostMapping
    public Bill createBill(@RequestBody Bill bill) {
        bills.add(bill);
        return bill;
    }

    @PutMapping("/{id}")
    public Bill updateBill(@PathVariable int id, @RequestBody Bill updatedBill) {
        for (Bill bill : bills) {
            if (bill.getId() == id) {
                bill.setAmount(updatedBill.getAmount());
                bill.setCustomerName(updatedBill.getCustomerName());
                return bill;
            }
        }
        return null;
    }

    @DeleteMapping("/{id}")
    public String deleteBill(@PathVariable int id) {
        bills.removeIf(bill -> bill.getId() == id);
        return "Bill with ID " + id + " has been deleted.";
    }

    @GetMapping("/api/customer/{customerName}")
    public List<Bill> getBillsByCustomerName(@PathVariable String customerName) {
        List<Bill> customerBills = new ArrayList<>();
        for (Bill bill : bills) {
            if (bill.getCustomerName().equalsIgnoreCase(customerName)) {
                customerBills.add(bill);
            }
        }
        return customerBills;
    }

    @GetMapping("/api/total")
    public double getTotalAmount() {
        return bills.stream().mapToDouble(Bill::getAmount).sum();
    }

    @GetMapping("/api/count")
    public int getBillCount() {
        return bills.size();
    }

    @GetMapping("/api/average")
    public double getAverageBillAmount() {
        return bills.stream().mapToDouble(Bill::getAmount).average().orElse(0.0);
    }

    @DeleteMapping
    public String deleteAllBills() {
        bills.clear();
        return "All bills have been deleted.";
    }
}

class Bill {
    private int id;
    private String customerName;
    private double amount;

    // Constructors, getters, and setters

    public Bill() {
    }

    public Bill(int id, String customerName, double amount) {
        this.id = id;
        this.customerName = customerName;
        this.amount = amount;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }
}