package com.rit.ems.controller;

import com.rit.ems.model.Batch;
import com.rit.ems.repository.BatchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/api/batches")
public class BatchController {

    @Autowired
    private BatchRepository batchRepository;

    @GetMapping
    public List<Batch> getAllBatches() {
        return batchRepository.findAll();
    }

    @PostMapping
    public ResponseEntity<?> createBatch(@RequestBody Batch batch) {
        if (batch.getId() == null) {
            batch.setId(System.currentTimeMillis() + new Random().nextInt(1000));
        }
        if (batch.getClasses() == null) {
            batch.setClasses(new java.util.ArrayList<>());
        }
        batchRepository.save(batch);
        return ResponseEntity.ok(batch);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteBatch(@PathVariable Long id) {
        batchRepository.deleteById(id);
        return ResponseEntity.ok(Map.of("message", "Batch deleted successfully"));
    }
}
