/*
**************************************************************************************
* Program Name  : DesignationController.java
* Author        : Kawade Swapnali
* Date          : Feb 12, 2026
* System Name   : gswbs
* Purpose       : REST Controller for Designation module.
* Called From   : Frontend (Angular)
* Beans Accessed: DesignationService
**************************************************************************************
*/

package com.coder.designation.controller;

import java.util.List;
import java.util.NoSuchElementException;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.coder.designation.model.Designation;
import com.coder.designation.model.SaveDesignationResponse;
import com.coder.designation.service.DesignationService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/designation_service")
public class DesignationController {
	
    @Autowired
    private final DesignationService designationService;

    /* FETCH SINGLE DESIGNATION */
    @GetMapping("/single/{prefix}/{year}/{code}")
    public ResponseEntity<?> getSingleDesignation(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code) {

        try {
            String designationId = prefix + "/" + year + "/" + code;
            Designation designation =
                    designationService.getSingleDesignation(designationId);

            return ResponseEntity.ok(designation);

        } catch (NoSuchElementException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(e.getMessage());
        }
    }
    
    /* FETCH ALL DESIGNATION */
    @GetMapping("/fetch-all-designation")
    public ResponseEntity<List<Designation>> fetchAllRecords(){
    	try {
    	   List<Designation> result = designationService.fetchAllDesignation();
    	   return new ResponseEntity<List<Designation>>(result,HttpStatus.OK);
	    }catch(NoSuchElementException e) {
	    	return new ResponseEntity<List<Designation>>(HttpStatus.NOT_FOUND);
	    }
    }

    /* FETCH ALL DESIGNATIONS BY LOGIN ID */
    @GetMapping("/fetchAllDesignation/{prefix}/{year}/{code}")
    public ResponseEntity<List<Designation>> getAllDesignationByLoginId(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code ) {
    	try {
    		String loginId = prefix + "/" + year + "/" + code;
    		List<Designation> result = designationService.getAllDesignationByLoginId(loginId);
    		return new ResponseEntity<List<Designation>>(result,HttpStatus.OK);
    	}
    	catch(NoSuchElementException e) {
    		return new ResponseEntity<List<Designation>>(HttpStatus.NOT_FOUND);
    	}
    }

    /* SAVE MULTIPLE DESIGNATIONS */
    @PostMapping("/saveAll")
    public ResponseEntity<SaveDesignationResponse> saveAllRecords(
            @RequestBody List<Designation> designationList) {

        SaveDesignationResponse result =
                designationService.saveAllDesignation(designationList);

        return ResponseEntity.ok(result);
    }

    /* UPDATE SINGLE DESIGNATION */
    @PutMapping("/update/{prefix}/{year}/{code}")
    public ResponseEntity<?> updateRecords(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code,
            @RequestBody Designation designation) {

        try {
            String designationId = prefix + "/" + year + "/" + code;
            Designation updated = designationService.update(designationId, designation);
            return ResponseEntity.ok(updated);

        } catch (NoSuchElementException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(e.getMessage());
        }
    }

    /* DELETE MULTIPLE DESIGNATIONS */
    @PostMapping("/delete-multiple-designation")
    public ResponseEntity<Boolean> deleteMultipleRecords(@RequestBody List<String> designationIds) {
        boolean result = designationService.multipalDelete(designationIds);
        return ResponseEntity.ok(result);
    }
    
    /* IMPORT THE ALL DESIGNATION INTO DESIGNATION MODULE USING DESIGNATION EXCEL */
	@PostMapping("/import-designation")
    public ResponseEntity<?> importExcel(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return ResponseEntity.badRequest().body("File is required");
        }
        try {
            List<Designation> saved = designationService.importFromExcel(file);
            return ResponseEntity.ok(saved);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Import failed: " + e.getMessage());
        }
    }
}	