package database

import (
	"fmt"
	"log"
	"time"

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

	"github.com/moustachioed/dash/backend/database/models"
)

type PgDatabase struct {
	Db *gorm.DB
}

func NewPgDatabase(host string, user string, password string, database string, port uint16) (Database, error) {
	db := &PgDatabase{}

	dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", host, user, password, database, port)
	gdb, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

	if err != nil {
		log.Print(err.Error())
		return nil, err
	}
	log.Printf("Connected to database.")

	db.Db = gdb

	db.migrate()

	return db, nil
}

func (db *PgDatabase) migrate() {
	db.Db.AutoMigrate(&models.Journal{})
}

func (db *PgDatabase) WriteJournalEntry(dbmodel models.Journal) error {
	return nil
}

func (db *PgDatabase) GetJournalEntryForDate(date time.Time) (models.Journal, error) {
	return models.Journal{}, nil
}
