package com.utopiaindustries.restcontroller;

import com.utopiaindustries.model.ctp.JobCard;
import com.utopiaindustries.model.ctp.JobCardItem;
import com.utopiaindustries.service.JobCardService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping( "/rest/job-cards" )
public class JobCardRestController {

    private final JobCardService jobCardService;

    public JobCardRestController(JobCardService jobCardService) {
        this.jobCardService = jobCardService;
    }

    @GetMapping( "/search" )
    public List<JobCard> getJobCards( @RequestParam(value = "term", required = false) String term,
                                      @RequestParam(value = "filter", required = false) Boolean filter){
       return jobCardService.searchJobCards( term , filter );
    }

    //search job card for avs integration
    @GetMapping( "/search-avs-jobcard" )
    public List<JobCard> searchJobCardByTermAndPOId( @RequestParam(value = "term", required = false) String term,
                                                     @RequestParam(value = "poId") long poID){
        return jobCardService.searchJobCardsByTermAndPoId( term , poID );
    }

    @GetMapping ( "/{id}/items" )
    public List<JobCardItem> getJobCardItems( @PathVariable( "id" ) long jobCardId ){
        return jobCardService.getJobCardItems( jobCardId );
    }

    @GetMapping( "/find/{id}" )
    public JobCard findById( @PathVariable("id") long cardId ){
        return jobCardService.findCardRecursively( cardId );
    }

    @PostMapping
    public String deleteJobCardItem(@RequestParam( "job-card-item-id" ) long jobCardItemId ){
        try{
            jobCardService.deleteJobCardItem( jobCardItemId );
            return "success";
        }catch ( Exception e ){
            return e.getMessage();
        }
    }

}
