package com.sino.mci.crm.controller;

import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.crm.dto.BindTagsRequest;
import com.sino.mci.crm.dto.CreateIdentityRequest;
import com.sino.mci.crm.dto.CreatePersonRequest;
import com.sino.mci.crm.dto.UpdatePersonRequest;
import com.sino.mci.crm.entity.Person;
import com.sino.mci.crm.service.PersonService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/person")
@RequiredArgsConstructor
public class PersonController {

    private final PersonService personService;

    @PostMapping
    public ApiResponse<?> createPerson(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody CreatePersonRequest request) {
        return ApiResponse.ok(personService.createPerson(tenantCode, request));
    }

    @GetMapping("/list")
    public ApiResponse<List<Person>> listPersons() {
        return ApiResponse.ok(personService.listPersons(AuthContext.currentTenantCode()));
    }

    @GetMapping("/{person_id}")
    public ApiResponse<?> getPerson(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId) {
        return ApiResponse.ok(personService.getPerson(tenantCode, personId));
    }

    @PutMapping("/{person_id}")
    public ApiResponse<?> updatePerson(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId,
            @Valid @RequestBody UpdatePersonRequest request) {
        return ApiResponse.ok(personService.updatePerson(tenantCode, personId, request));
    }

    @DeleteMapping("/{person_id}")
    public ApiResponse<?> deletePerson(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId) {
        personService.deletePerson(tenantCode, personId);
        return ApiResponse.ok();
    }

    @PostMapping("/{person_id}/identities")
    public ApiResponse<?> createIdentity(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId,
            @Valid @RequestBody CreateIdentityRequest request) {
        request.setPersonId(personId);
        return ApiResponse.ok(personService.createIdentity(tenantCode, request));
    }

    @GetMapping("/{person_id}/identities")
    public ApiResponse<?> listIdentities(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId) {
        return ApiResponse.ok(personService.listIdentities(tenantCode, personId));
    }

    @DeleteMapping("/{person_id}/identities/{identity_id}")
    public ApiResponse<?> deleteIdentity(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId,
            @PathVariable("identity_id") Long identityId) {
        personService.deleteIdentity(tenantCode, personId, identityId);
        return ApiResponse.ok();
    }

    @PostMapping("/{person_id}/bind-tags")
    public ApiResponse<?> bindTags(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("person_id") Long personId,
            @Valid @RequestBody BindTagsRequest request) {
        personService.bindTags(tenantCode, personId, request);
        return ApiResponse.ok();
    }
}
