package com.example.cezenPBX.controller;
import com.example.cezenPBX.DTO.*;
import com.example.cezenPBX.service.BasicPbxServiceInterface;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cezen")
public class CezenPbxController {

    @Autowired
    private BasicPbxServiceInterface basicPbxServiceInterface;


    @PostMapping("/add_user")
    // this method will accept json input fields, process them using jackson then pass them to the service
    public boolean createNewEndPointController(@RequestBody @Valid PsEndPointsDTO psEndPointsRecordDTO){

        System.out.println(psEndPointsRecordDTO);

        // now pass the DTO data to the service

        return this.basicPbxServiceInterface.createANewEndpointService(
                psEndPointsRecordDTO.id(),
                psEndPointsRecordDTO.transport(),
                // because the id and AORS and AUTHS are the same and have to be so
                psEndPointsRecordDTO.id(),
                psEndPointsRecordDTO.id(),
                psEndPointsRecordDTO.context(),
                psEndPointsRecordDTO.disallow(),
                psEndPointsRecordDTO.allow(),
                psEndPointsRecordDTO.directMedia(),
                psEndPointsRecordDTO.connectedLineMethod(),
                psEndPointsRecordDTO.callerid(),
                psEndPointsRecordDTO.dtmfMode(),
                psEndPointsRecordDTO.mohsuggest(),
                psEndPointsRecordDTO.mailboxes()
        );
        // return false;
    }

    @PostMapping("/add_extension")
    public ReturnStatus addANewExtensionToDatabase(@RequestBody ExtensionsDTO extensionsDTO){

        System.out.println(extensionsDTO);
        return this.basicPbxServiceInterface.addExtensionForUser(
                extensionsDTO.context(),
                extensionsDTO.extension(),
                extensionsDTO.priority(),
                extensionsDTO.app(),
                extensionsDTO.appdata());
    }


    @PostMapping("/set_password")
    public  ReturnStatus setPasswordForSipEndpoint(@RequestBody SipPasswordDTO sipPasswordDTO){

        return this.basicPbxServiceInterface.addAPasswordForEndpoint(
                sipPasswordDTO.id(),
                sipPasswordDTO.authType(),
                sipPasswordDTO.userName(),
                sipPasswordDTO.password(),
                sipPasswordDTO.md5Cred(),
                sipPasswordDTO.realm()
        );
    }

    @PostMapping("/set_aors")
    public ReturnStatus setAorsForSipEndpoint(@RequestBody PsAorsDTO psAorsDTO){

        return this.basicPbxServiceInterface.setAors(
                psAorsDTO.id(),
                psAorsDTO.maxContacts()
        );
    }

    @DeleteMapping("/delete_extension")
    public boolean deleteExtensionBySipNumber(@RequestParam String sipNumber){

        return this.basicPbxServiceInterface.deleteByExtension(sipNumber);
    }

    // add global extension features like voicemail
    @PutMapping("/add_feature")
    public ReturnStatus addAGlobalFeatureToExtension(@RequestBody ExtensionsDTO extensionsDTO){

        return this.basicPbxServiceInterface.addFeaturesToExtensionsForEndpoints(
                extensionsDTO.context(),
                extensionsDTO.extension(),
                extensionsDTO.priority(),
                extensionsDTO.app(),
                extensionsDTO.appdata());
    }

}
