package gitlin

import (
  "fmt"
  "os"
  "os/user"

  "github.com/spf13/cobra"
)

var setupCmd = &cobra.Command{
  Use:   "setup",
  Short: "Interactive Gitlin instance setup wizard",
  Long:  `This command allows you to setup your Gitlin instance in an interactive way. It will ask you a few questions and then set environment variables for you.`,
  Run: func(cmd *cobra.Command, args []string) {
    setup()
  },
}

func init() {
  rootCmd.AddCommand(setupCmd)
}

type Vars struct {
  IPLogging, URLLogging, UserAgentLogging, DiagnosticLogging bool
  PrivacyPolicy, InstanceCountry, InstanceProvider, CloudflareStatus, Proxying string
}

func setup() {
  cur, err := user.Current()
  if err != nil {
    fmt.Println("現在のユーザーのUIDを見つけられませんでした。", err)
    return
  }

  if cur.Uid != "0" {
    fmt.Println("rootとして実行して下さい。 sudo gitlin setup 又は doas gitlin setup")
    return
  }

  fmt.Println("Gitlin setup wizard")
  fmt.Println("This wizard will help you setup your Gitlin instance.")

  // set up variables
  vars := Vars{}

  // ask user for information about IP logging
  fmt.Print("Do you log IP addresses of users? (y/N) ")
  var ipLogging string
  fmt.Scanln(&ipLogging)
  if ipLogging == "y" {
    // create environment variable
    vars.IPLogging = true
  } else {
    // create environment variable
    vars.IPLogging = false
  }

  // ask user for information about URL request logging
  fmt.Print("Do you log URL requests of users? (y/N) ")
  var urlLogging string
  fmt.Scanln(&urlLogging)
  if urlLogging == "y" {
    vars.URLLogging = true
  } else {
    vars.URLLogging = false
  }

  // ask user for information about User-Agent logging
  fmt.Print("Do you log your users User-Agent? (y/N) ")
  var uaLogging string
  fmt.Scanln(&uaLogging)
  if uaLogging == "y" {
    vars.UserAgentLogging = true
  } else {
    vars.UserAgentLogging = false
  }

  // ask user for information about diagnostic information logging
  fmt.Print("Do you log diagnostic information? (y/N) ")
  var diagLogging string
  fmt.Scanln(&diagLogging)
  if diagLogging == "y" {
    vars.DiagnosticLogging = true
  } else {
    vars.DiagnosticLogging = false
  }

  // ask user about privacy policy
  fmt.Print("Do you have a privacy policy? (y/N) ")
  var privacyPolicy string
  fmt.Scanln(&privacyPolicy)
  if privacyPolicy == "y" {
    // ask user for privacy policy URL
    fmt.Print("Please enter the URL of your privacy policy. ")
    var privacyPolicyURL string
    fmt.Scanln(&privacyPolicyURL)
    vars.PrivacyPolicy = privacyPolicyURL
  } else {
    vars.PrivacyPolicy = ""
  }

  // ask user about the country they're hosting from
  fmt.Print("Please enter the country you're hosting from. ")
  var country string
  fmt.Scanln(&country)
  if country != "" {
    vars.InstanceCountry = country
  } else {
    vars.InstanceCountry = ""
  }

  // ask user about their ISP/Hosting provider
  fmt.Print("Please enter your ISP/Hosting provider. ")
  var isp string
  fmt.Scanln(&isp)
  if isp != "" {
    vars.InstanceProvider = isp
  } else {
    vars.InstanceProvider = ""
  }

  // ask user about Cloudflare status
  fmt.Print("Are you using Cloudflare? (y/N) ")
  var cloudflare string
  fmt.Scanln(&cloudflare)
  if cloudflare == "y" {
    vars.CloudflareStatus = "true"
  } else {
    vars.CloudflareStatus = "false"
  }

  // ask user whether they want proxying enabled
  fmt.Print("Do you want proxying enabled? (Y/n) ")
  var proxying string
  fmt.Scanln(&proxying)
  if proxying == "y" || proxying == "" {
    vars.Proxying = "true"
  } else {
    vars.Proxying = "false"
  }

  // save to gitlin.conf file
  fmt.Println("Saving environment variables to config file...")
  f, err := os.Create("/etc/gitlin/gitlin.conf")
  if err != nil {
    fmt.Println("Error creating config file: ", err)
    os.Exit(1)
  }
  defer f.Close()

  // write to file. make it look nice
  f.WriteString("# Gitlin environment variables\n")
  f.WriteString("# This file was generated by the setup wizard. You can edit it manually if you want.\n")
  f.WriteString("GITLIN_SETUP_COMPLETE=true\n")
  f.WriteString("GITLIN_IP_LOGGED=" + fmt.Sprint(vars.IPLogging) + "\n")
  f.WriteString("GITLIN_REQUEST_URL_LOGGED=" + fmt.Sprint(vars.URLLogging) + "\n")
  f.WriteString("GITLIN_USER_AGENT_LOGGED=" + fmt.Sprint(vars.UserAgentLogging) + "\n")
  f.WriteString("GITLIN_DIAGNOSTIC_INFO_LOGGED=" + fmt.Sprint(vars.DiagnosticLogging) + "\n")
  f.WriteString("GITLIN_INSTANCE_PRIVACY_POLICY=" + vars.PrivacyPolicy + "\n")
  f.WriteString("GITLIN_INSTANCE_COUNTRY=" + vars.InstanceCountry + "\n")
  f.WriteString("GITLIN_INSTANCE_PROVIDER=" + vars.InstanceProvider + "\n")
  f.WriteString("GITLIN_INSTANCE_CLOUDFLARE=" + vars.CloudflareStatus + "\n")
  f.WriteString("GITLIN_PROXYING_ENABLED=" + vars.Proxying + "\n")

  println("All done! You can now start your Gitlin instance with 'gitlin serve'.")
  println("You can review the environment variables with 'gitlin env'.")
}
