package cli

import (
	"database/sql"
	_ "embed"
	"errors"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	_ "modernc.org/sqlite"
)

//go:embed sqlite_schema.sql
var sqliteSchema string

func runDB(root string, args []string) error {
	if len(args) == 0 {
		return errors.New("missing db subcommand")
	}
	switch args[0] {
	case "init":
		return runDBInit(root, args[1:])
	case "status":
		return runDBStatus(root, args[1:])
	default:
		return fmt.Errorf("unknown db subcommand %q", args[0])
	}
}

func runDBInit(root string, args []string) error {
	fs := flag.NewFlagSet("db init", flag.ContinueOnError)
	fs.SetOutput(os.Stderr)
	path := fs.String("path", defaultSQLitePath, "SQLite database path")
	if err := fs.Parse(args); err != nil {
		return err
	}

	dbPath := resolveRootPath(root, *path)
	if err := os.MkdirAll(filepath.Dir(dbPath), 0o755); err != nil {
		return err
	}

	db, err := openSQLite(dbPath)
	if err != nil {
		return err
	}
	defer db.Close()

	if err := applySQLiteSchema(db); err != nil {
		return err
	}

	fmt.Printf("initialized SQLite database: %s\n", mustRel(root, dbPath))
	return nil
}

func runDBStatus(root string, args []string) error {
	fs := flag.NewFlagSet("db status", flag.ContinueOnError)
	fs.SetOutput(os.Stderr)
	path := fs.String("path", defaultSQLitePath, "SQLite database path")
	if err := fs.Parse(args); err != nil {
		return err
	}

	dbPath := resolveRootPath(root, *path)
	info, err := os.Stat(dbPath)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			fmt.Printf("database: %s\n", mustRel(root, dbPath))
			fmt.Println("exists: no")
			return nil
		}
		return err
	}

	db, err := openSQLite(dbPath)
	if err != nil {
		return err
	}
	defer db.Close()

	fmt.Printf("database: %s\n", mustRel(root, dbPath))
	fmt.Println("exists: yes")
	fmt.Printf("size: %d bytes\n", info.Size())
	for _, table := range []string{"posts", "settings", "sync_state"} {
		ok, err := sqliteTableExists(db, table)
		if err != nil {
			return err
		}
		fmt.Printf("table %-10s %s\n", table+":", yesNo(ok))
	}
	return nil
}

func openSQLite(path string) (*sql.DB, error) {
	db, err := sql.Open("sqlite", path)
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		db.Close()
		return nil, err
	}
	return db, nil
}

func openProjectSQLite(root string, path string) (*sql.DB, string, error) {
	dbPath := resolveRootPath(root, path)
	if _, err := os.Stat(dbPath); err != nil {
		if errors.Is(err, os.ErrNotExist) {
			return nil, "", fmt.Errorf("database does not exist: %s; run `osaetctl db init` first", mustRel(root, dbPath))
		}
		return nil, "", err
	}
	db, err := openSQLite(dbPath)
	if err != nil {
		return nil, "", err
	}
	if err := applySQLiteSchema(db); err != nil {
		db.Close()
		return nil, "", err
	}
	return db, dbPath, nil
}

func applySQLiteSchema(db *sql.DB) error {
	for _, statement := range strings.Split(sqliteSchema, ";") {
		statement = strings.TrimSpace(statement)
		if statement == "" {
			continue
		}
		if _, err := db.Exec(statement); err != nil {
			return err
		}
	}
	return nil
}

func sqliteTableExists(db *sql.DB, table string) (bool, error) {
	var count int
	err := db.QueryRow(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?`, table).Scan(&count)
	return count > 0, err
}
