package com.emonster.taroaichat.web.rest;

import com.emonster.taroaichat.repository.TarotSessionRepository;
import com.emonster.taroaichat.service.TarotSessionQueryService;
import com.emonster.taroaichat.service.TarotSessionService;
import com.emonster.taroaichat.service.UserProfileService;
import com.emonster.taroaichat.service.criteria.TarotSessionCriteria;
import com.emonster.taroaichat.service.dto.TarotSessionDTO;
import com.emonster.taroaichat.web.rest.errors.BadRequestAlertException;
import com.emonster.taroaichat.web.rest.vm.SessionFeedbackVM;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.PaginationUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.emonster.taroaichat.domain.TarotSession}.
 */
@RestController
@RequestMapping("/api/v1/tarot-sessions")
public class TarotSessionResource {

    private static final Logger LOG = LoggerFactory.getLogger(TarotSessionResource.class);

    private static final String ENTITY_NAME = "tarotSession";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final TarotSessionService tarotSessionService;

    private final TarotSessionRepository tarotSessionRepository;

    private final TarotSessionQueryService tarotSessionQueryService;

    private final UserProfileService userProfileService;

    public TarotSessionResource(
        TarotSessionService tarotSessionService,
        TarotSessionRepository tarotSessionRepository,
        TarotSessionQueryService tarotSessionQueryService,
        UserProfileService userProfileService
    ) {
        this.tarotSessionService = tarotSessionService;
        this.tarotSessionRepository = tarotSessionRepository;
        this.tarotSessionQueryService = tarotSessionQueryService;
        this.userProfileService = userProfileService;
    }

    /**
     * {@code POST  /tarot-sessions} : Create a new tarotSession.
     *
     * @param tarotSessionDTO the tarotSessionDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new tarotSessionDTO, or with status {@code 400 (Bad Request)} if the tarotSession has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("")
    public ResponseEntity<TarotSessionDTO> createTarotSession(@Valid @RequestBody TarotSessionDTO tarotSessionDTO)
        throws URISyntaxException {
        LOG.debug("REST request to save TarotSession : {}", tarotSessionDTO);
        if (tarotSessionDTO.getId() != null) {
            throw new BadRequestAlertException("A new tarotSession cannot already have an ID", ENTITY_NAME, "idexists");
        }
        tarotSessionDTO = tarotSessionService.save(tarotSessionDTO);
        return ResponseEntity.created(new URI("/api/tarot-sessions/" + tarotSessionDTO.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, tarotSessionDTO.getId().toString()))
            .body(tarotSessionDTO);
    }

    /**
     * {@code PUT  /tarot-sessions/:id} : Updates an existing tarotSession.
     *
     * @param id the id of the tarotSessionDTO to save.
     * @param tarotSessionDTO the tarotSessionDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated tarotSessionDTO,
     * or with status {@code 400 (Bad Request)} if the tarotSessionDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the tarotSessionDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/{id}")
    public ResponseEntity<TarotSessionDTO> updateTarotSession(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody TarotSessionDTO tarotSessionDTO
    ) throws URISyntaxException {
        LOG.debug("REST request to update TarotSession : {}, {}", id, tarotSessionDTO);
        if (tarotSessionDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, tarotSessionDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!tarotSessionRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        tarotSessionDTO = tarotSessionService.update(tarotSessionDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, tarotSessionDTO.getId().toString()))
            .body(tarotSessionDTO);
    }

    /**
     * {@code PATCH  /tarot-sessions/:id} : Partial updates given fields of an existing tarotSession, field will ignore if it is null
     *
     * @param id the id of the tarotSessionDTO to save.
     * @param tarotSessionDTO the tarotSessionDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated tarotSessionDTO,
     * or with status {@code 400 (Bad Request)} if the tarotSessionDTO is not valid,
     * or with status {@code 404 (Not Found)} if the tarotSessionDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the tarotSessionDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<TarotSessionDTO> partialUpdateTarotSession(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody TarotSessionDTO tarotSessionDTO
    ) throws URISyntaxException {
        LOG.debug("REST request to partial update TarotSession partially : {}, {}", id, tarotSessionDTO);
        if (tarotSessionDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, tarotSessionDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!tarotSessionRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<TarotSessionDTO> result = tarotSessionService.partialUpdate(tarotSessionDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, tarotSessionDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /tarot-sessions} : get all the tarotSessions.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of tarotSessions in body.
     */
    @GetMapping("")
    public ResponseEntity<List<TarotSessionDTO>> getAllTarotSessions(
        TarotSessionCriteria criteria,
        @org.springdoc.core.annotations.ParameterObject Pageable pageable
    ) {
        LOG.debug("REST request to get TarotSessions by criteria: {}", criteria);

        Page<TarotSessionDTO> page = tarotSessionQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * {@code GET  /tarot-sessions/count} : count all the tarotSessions.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/count")
    public ResponseEntity<Long> countTarotSessions(TarotSessionCriteria criteria) {
        LOG.debug("REST request to count TarotSessions by criteria: {}", criteria);
        return ResponseEntity.ok().body(tarotSessionQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /tarot-sessions/:id} : get the "id" tarotSession.
     *
     * @param id the id of the tarotSessionDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the tarotSessionDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/{id}")
    public ResponseEntity<TarotSessionDTO> getTarotSession(@PathVariable("id") Long id) {
        LOG.debug("REST request to get TarotSession : {}", id);
        Optional<TarotSessionDTO> tarotSessionDTO = tarotSessionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tarotSessionDTO);
    }

    /**
     * {@code DELETE  /tarot-sessions/:id} : delete the "id" tarotSession.
     *
     * @param id the id of the tarotSessionDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTarotSession(@PathVariable("id") Long id) {
        LOG.debug("REST request to delete TarotSession : {}", id);
        tarotSessionService.delete(id);
        return ResponseEntity.noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }

    /**
     * {@code PATCH  /tarot-sessions/:id/feedback} : Update feedback for a session.
     *
     * @param id the id of the tarotSession to update feedback for.
     * @param feedbackVM the feedback and rating to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated tarotSessionDTO,
     * or with status {@code 403 (Forbidden)} if the user doesn't own the session,
     * or with status {@code 404 (Not Found)} if the session is not found.
     */
    @PatchMapping("/{id}/feedback")
    public ResponseEntity<TarotSessionDTO> updateSessionFeedback(
        @PathVariable("id") Long id,
        @Valid @RequestBody SessionFeedbackVM feedbackVM
    ) {
        LOG.debug("REST request to update feedback for TarotSession : {}", id);
        
        // Get current user's profile ID
        Long currentUserProfileId = userProfileService.getCurrentUserProfileId()
            .orElseThrow(() -> new BadRequestAlertException("User profile not found", ENTITY_NAME, "userprofilenotfound"));
        
        Optional<TarotSessionDTO> result = tarotSessionService.updateSessionFeedback(
            id, 
            feedbackVM.getFeedback(), 
            feedbackVM.getRating(),
            currentUserProfileId
        );
        
        if (result.isEmpty()) {
            throw new BadRequestAlertException("Session not found or you don't have permission to update it", ENTITY_NAME, "sessionnotfoundorforbidden");
        }
        
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .body(result.get());
    }
}
