package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/lib/pq"
)

func main() {
	dsn := "host=31.97.228.132 user=admin password=Package@321# dbname=logistics port=5433 sslmode=disable"
	db, err := sql.Open("postgres", dsn)
	if err != nil {
		log.Fatalf("Open failed: %v", err)
	}
	defer db.Close()

	if err := db.Ping(); err != nil {
		log.Fatalf("Ping failed: %v", err)
	}
	fmt.Println("Connected OK")

	// Drop the old constraint
	_, err = db.Exec(`ALTER TABLE pickupbookings DROP CONSTRAINT IF EXISTS pickupbookings_status_check`)
	if err != nil {
		log.Fatalf("Drop constraint failed: %v", err)
	}
	fmt.Println("Old constraint dropped.")

	// Recreate with all status values from constants
	_, err = db.Exec(`
		ALTER TABLE pickupbookings ADD CONSTRAINT pickupbookings_status_check
		CHECK (status IN (
			'Pending_Pickup',
			'Created',
			'Miler_Assigned',
			'Pickup_Scheduled',
			'Picked_Up',
			'Converted_To_Consignment',
			'Cancelled'
		))
	`)
	if err != nil {
		log.Fatalf("Add constraint failed: %v", err)
	}
	fmt.Println("New constraint added with Pending_Pickup included.")

	// Verify
	var def string
	db.QueryRow(`
		SELECT pg_get_constraintdef(con.oid)
		FROM pg_constraint con
		JOIN pg_class rel ON rel.oid = con.conrelid
		WHERE rel.relname = 'pickupbookings' AND con.contype = 'c'
	`).Scan(&def)
	fmt.Println("Current constraint:", def)
}
