import type { ColumnType, Insertable, Selectable, Updateable } from "kysely"
import type { AutomationConfig } from "../types"
import type { ActionInstancesId } from "./ActionInstances"
import type { Event } from "./Event"

import { z } from "zod"

import { actionInstancesIdSchema } from "./ActionInstances"
import { eventSchema } from "./Event"

// @generated
// This file is automatically generated by Kanel. Do not modify manually.

/** Identifier type for public.automations */
export type AutomationsId = string & { __brand: "AutomationsId" }

/** Represents the table public.automations */
export interface AutomationsTable {
	id: ColumnType<AutomationsId, AutomationsId | undefined, AutomationsId>

	event: ColumnType<Event, Event, Event>

	actionInstanceId: ColumnType<ActionInstancesId, ActionInstancesId, ActionInstancesId>

	config: ColumnType<AutomationConfig | null, AutomationConfig | null, AutomationConfig | null>

	createdAt: ColumnType<Date, Date | string | undefined, Date | string>

	updatedAt: ColumnType<Date, Date | string | undefined, Date | string>

	sourceActionInstanceId: ColumnType<
		ActionInstancesId | null,
		ActionInstancesId | null,
		ActionInstancesId | null
	>
}

export type Automations = Selectable<AutomationsTable>

export type NewAutomations = Insertable<AutomationsTable>

export type AutomationsUpdate = Updateable<AutomationsTable>

export const automationsIdSchema = z.string().uuid() as unknown as z.Schema<AutomationsId>

export const automationsSchema = z.object({
	id: automationsIdSchema,
	event: eventSchema,
	actionInstanceId: actionInstancesIdSchema,
	config: z.unknown().nullable(),
	createdAt: z.date(),
	updatedAt: z.date(),
	sourceActionInstanceId: actionInstancesIdSchema.nullable(),
})

export const automationsInitializerSchema = z.object({
	id: automationsIdSchema.optional(),
	event: eventSchema,
	actionInstanceId: actionInstancesIdSchema,
	config: z.unknown().optional().nullable(),
	createdAt: z.date().optional(),
	updatedAt: z.date().optional(),
	sourceActionInstanceId: actionInstancesIdSchema.optional().nullable(),
})

export const automationsMutatorSchema = z.object({
	id: automationsIdSchema.optional(),
	event: eventSchema.optional(),
	actionInstanceId: actionInstancesIdSchema.optional(),
	config: z.unknown().optional().nullable(),
	createdAt: z.date().optional(),
	updatedAt: z.date().optional(),
	sourceActionInstanceId: actionInstancesIdSchema.optional().nullable(),
})
