package Database

import (
	"log"

	"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

const dbUrl = "postgres://postgres:@localhost:5432/sudden_impact_records"

var (
	DB *gorm.DB
)

func Init() {
	var (
		err error
	)

	log.Println("Initializing database...")

	DB, err = gorm.Open(postgres.Open(dbUrl), &gorm.Config{})

	if err != nil {
		log.Fatalln(err)
	}

	log.Println("Running AutoMigrate on Post tables...")

	// Post tables
	DB.AutoMigrate(&Models.PostImage{})
	DB.AutoMigrate(&Models.PostVideo{})
	DB.AutoMigrate(&Models.PostAudio{})
	DB.AutoMigrate(&Models.PostLink{})
	DB.AutoMigrate(&Models.Post{})

	log.Println("Running AutoMigrate on Subscription tables...")

	// Email subscription tables
	DB.AutoMigrate(&Models.SubscriptionEmailAttachment{})
	DB.AutoMigrate(&Models.SubscriptionEmail{})
	DB.AutoMigrate(&Models.Subscription{})
}
