package boot

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"log"
	"github.com/aws/aws-lambda-go/events"
	CRON "cf-capital/module_server/handlers/commands"
)

type responseBody struct{
	Status string `json:"status"`
	Message string `json:"message"`
}


func boot(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error){
	fmt.Print("Booting CFC Daily Cron")
	ApiResponse := events.APIGatewayProxyResponse{}
	errorResponse := responseBody{Status: "FAIL", Message: "Invalid request received"}

	log.Println(request)
	log.Println(os.Getenv("CronType"))

    if os.Getenv("CronType") == "daily_investment" {
        fmt.Print("CFC Cron Start")
        CRON.Daily()
        fmt.Print("CFC Cron Ended")
    }

    if os.Getenv("CronType") == "send_maturity_mails" {
        fmt.Print("Maturity investment Start")
        CRON.SendMaturityMails()
        fmt.Print("Maturity investment Ended")
    }

    if os.Getenv("CronType") == "abort_transactions" {
        fmt.Print("Abort transactions Start")
        CRON.AbortTransactions()
        fmt.Print("Abort transactions Ended")
    }

	bytes, _ := json.Marshal(errorResponse)
	ApiResponse = events.APIGatewayProxyResponse{Body: string(bytes), StatusCode: 400}
	return ApiResponse, nil
}

func Start(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error){
	return boot(ctx, request)
}
