/*
**************************************************************************************
* Program Name  : AssetStatusChangeController.java
* Author        : Kawade Swapnali
* Date          : 2026
* Purpose       : REST Controller for Asset Status Change module
**************************************************************************************
*/

package com.coder.asset.status.change.controller;

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

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

import com.coder.asset.status.change.dto.ApiResponse;
import com.coder.asset.status.change.dto.AssetStatusChangeDTO;
import com.coder.asset.status.change.model.AssetStatusChange;
import com.coder.asset.status.change.model.SaveToAssetStatusChangeResponse;
import com.coder.asset.status.change.service.AssetStatusChangeService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/asset_status_change_service")
public class AssetStatusChangeController {

    @Autowired
    private final AssetStatusChangeService service;

    /* ================= FETCH SINGLE ================= */
    @GetMapping("/single/{prefix}/{year}/{code}/{loginId}")
    public ResponseEntity<ApiResponse<AssetStatusChangeDTO>> getSingleStatusChange(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code,
            @PathVariable String loginId
    ) {
        try {
            String id = prefix + "/" + year + "/" + code;

            AssetStatusChangeDTO result =
                    service.getSingleStatusChange(id);

            return ResponseEntity.ok(
                    ApiResponse.success("Status Change fetched successfully", result)
            );

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

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    /* ================= FETCH ALL ================= */
    @GetMapping("/fetch-all")
    public ResponseEntity<ApiResponse<List<AssetStatusChangeDTO>>> fetchAllStatusChanges() {
        try {
            List<AssetStatusChangeDTO> result =
                    service.fetchAllStatusChanges();

            return ResponseEntity.ok(
                    ApiResponse.success("All status changes fetched successfully", result)
            );

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    /* ================= FETCH BY LOGIN ================= */
    @GetMapping("/getAllAssetStatusChangeByLoginId/{loginId}")
    public ResponseEntity<ApiResponse<List<AssetStatusChangeDTO>>> getAllByLogin(
            @PathVariable String loginId
    ) {
        try {
            List<AssetStatusChangeDTO> result =
                    service.getAllStatusChangesByCreatedBy(loginId);

            return ResponseEntity.ok(
                    ApiResponse.success("Status changes fetched successfully", result)
            );

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    /* ================= SAVE ================= */
    @PostMapping("/saveAll")
    public ResponseEntity<ApiResponse<SaveToAssetStatusChangeResponse>> saveAll(
            @RequestBody List<AssetStatusChange> list
    ) {
        try {
            SaveToAssetStatusChangeResponse result =
                    service.saveAllStatusChanges(list);

            return ResponseEntity.ok(
                    ApiResponse.success("Status changes saved successfully", result)
            );

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    /* ================= UPDATE ================= */
    @PutMapping("/update/{prefix}/{year}/{code}")
    public ResponseEntity<?> update(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code,
            @RequestBody AssetStatusChange data
    ) {
        try {
            String id = prefix + "/" + year + "/" + code;

            AssetStatusChange updated = service.update(id, data);

            return new ResponseEntity<>(updated, HttpStatus.OK);

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

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(e.getMessage());
        }
    }

    /* ================= DELETE MULTIPLE ================= */
    @PostMapping("/delete-multiple")
    public ResponseEntity<?> deleteMultiple(@RequestBody List<String> ids) {
        try {
            return ResponseEntity.ok(service.multipleDelete(ids));

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(e.getMessage());
        }
    }

    /* ================= DELETE SINGLE ================= */
    @DeleteMapping("/delete/{prefix}/{year}/{code}")
    public ResponseEntity<?> deleteSingle(
            @PathVariable String prefix,
            @PathVariable String year,
            @PathVariable String code
    ) {
        try {
            String id = prefix + "/" + year + "/" + code;

            return ResponseEntity.ok(service.deleteById(id));

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(e.getMessage());
        }
    }
}