CREATE TABLE "jwks" (
	"id" text PRIMARY KEY NOT NULL,
	"public_key" text NOT NULL,
	"private_key" text NOT NULL,
	"created_at" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE "charge_point" (
	"id" varchar PRIMARY KEY NOT NULL,
	"charge_point_identifier" varchar(255) NOT NULL,
	"charge_point_serial_number" varchar(25),
	"charge_point_model" varchar(20) NOT NULL,
	"charge_point_vendor" varchar(20) NOT NULL,
	"firmware_version" varchar(50),
	"iccid" varchar(20),
	"imsi" varchar(20),
	"meter_serial_number" varchar(25),
	"meter_type" varchar(25),
	"registration_status" varchar DEFAULT 'Pending' NOT NULL,
	"heartbeat_interval" integer DEFAULT 60,
	"last_heartbeat_at" timestamp with time zone,
	"last_boot_notification_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "charge_point_charge_point_identifier_unique" UNIQUE("charge_point_identifier")
);
--> statement-breakpoint
CREATE TABLE "charging_profile" (
	"id" integer PRIMARY KEY NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"connector_id" varchar,
	"connector_number" integer NOT NULL,
	"transaction_id" integer,
	"stack_level" integer NOT NULL,
	"charging_profile_purpose" varchar NOT NULL,
	"charging_profile_kind" varchar NOT NULL,
	"recurrency_kind" varchar,
	"valid_from" timestamp with time zone,
	"valid_to" timestamp with time zone,
	"charging_schedule" jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "connector" (
	"id" varchar PRIMARY KEY NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"connector_id" integer NOT NULL,
	"status" varchar DEFAULT 'Unavailable' NOT NULL,
	"error_code" varchar DEFAULT 'NoError' NOT NULL,
	"info" varchar(50),
	"vendor_id" varchar(255),
	"vendor_error_code" varchar(50),
	"last_status_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "connector_status_history" (
	"id" varchar PRIMARY KEY NOT NULL,
	"connector_id" varchar NOT NULL,
	"connector_number" integer NOT NULL,
	"status" varchar NOT NULL,
	"error_code" varchar NOT NULL,
	"info" varchar(50),
	"vendor_id" varchar(255),
	"vendor_error_code" varchar(50),
	"status_timestamp" timestamp with time zone,
	"received_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "id_tag" (
	"id_tag" varchar(20) PRIMARY KEY NOT NULL,
	"parent_id_tag" varchar(20),
	"status" varchar DEFAULT 'Accepted' NOT NULL,
	"expiry_date" timestamp with time zone,
	"user_id" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "local_auth_list" (
	"id" varchar PRIMARY KEY NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"list_version" integer NOT NULL,
	"id_tag" varchar(20) NOT NULL,
	"parent_id_tag" varchar(20),
	"id_tag_status" varchar NOT NULL,
	"expiry_date" timestamp with time zone,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "meter_value" (
	"id" varchar PRIMARY KEY NOT NULL,
	"transaction_id" integer,
	"connector_id" varchar NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"connector_number" integer NOT NULL,
	"timestamp" timestamp with time zone NOT NULL,
	"sampled_values" jsonb NOT NULL,
	"received_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "reservation" (
	"id" integer PRIMARY KEY NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"connector_id" varchar,
	"connector_number" integer NOT NULL,
	"expiry_date" timestamp with time zone NOT NULL,
	"id_tag" varchar(20) NOT NULL,
	"parent_id_tag" varchar(20),
	"status" varchar DEFAULT 'Active' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "transaction" (
	"id" serial PRIMARY KEY NOT NULL,
	"charge_point_id" varchar NOT NULL,
	"connector_id" varchar NOT NULL,
	"connector_number" integer NOT NULL,
	"id_tag" varchar(20) NOT NULL,
	"id_tag_status" varchar,
	"start_timestamp" timestamp with time zone NOT NULL,
	"start_meter_value" integer NOT NULL,
	"stop_id_tag" varchar(20),
	"stop_timestamp" timestamp with time zone,
	"stop_meter_value" integer,
	"stop_reason" varchar,
	"reservation_id" integer,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "role" DROP DEFAULT;--> statement-breakpoint
ALTER TABLE "session" ADD COLUMN "impersonated_by" text;--> statement-breakpoint
ALTER TABLE "user" ADD COLUMN "banned" boolean DEFAULT false;--> statement-breakpoint
ALTER TABLE "user" ADD COLUMN "ban_reason" text;--> statement-breakpoint
ALTER TABLE "user" ADD COLUMN "ban_expires" timestamp;--> statement-breakpoint
ALTER TABLE "charging_profile" ADD CONSTRAINT "charging_profile_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "charging_profile" ADD CONSTRAINT "charging_profile_connector_id_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."connector"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "charging_profile" ADD CONSTRAINT "charging_profile_transaction_id_transaction_id_fk" FOREIGN KEY ("transaction_id") REFERENCES "public"."transaction"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "connector" ADD CONSTRAINT "connector_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "connector_status_history" ADD CONSTRAINT "connector_status_history_connector_id_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."connector"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "id_tag" ADD CONSTRAINT "id_tag_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "local_auth_list" ADD CONSTRAINT "local_auth_list_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "meter_value" ADD CONSTRAINT "meter_value_transaction_id_transaction_id_fk" FOREIGN KEY ("transaction_id") REFERENCES "public"."transaction"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "meter_value" ADD CONSTRAINT "meter_value_connector_id_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."connector"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "meter_value" ADD CONSTRAINT "meter_value_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reservation" ADD CONSTRAINT "reservation_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reservation" ADD CONSTRAINT "reservation_connector_id_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."connector"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_charge_point_id_charge_point_id_fk" FOREIGN KEY ("charge_point_id") REFERENCES "public"."charge_point"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_connector_id_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."connector"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_charging_profile_charge_point_id" ON "charging_profile" USING btree ("charge_point_id");--> statement-breakpoint
CREATE INDEX "idx_charging_profile_connector_id" ON "charging_profile" USING btree ("connector_id");--> statement-breakpoint
CREATE INDEX "idx_charging_profile_purpose_stack" ON "charging_profile" USING btree ("connector_number","charging_profile_purpose","stack_level");--> statement-breakpoint
CREATE INDEX "idx_connector_charge_point_id" ON "connector" USING btree ("charge_point_id");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_connector_charge_point_connector" ON "connector" USING btree ("charge_point_id","connector_id");--> statement-breakpoint
CREATE INDEX "idx_status_history_connector_id" ON "connector_status_history" USING btree ("connector_id");--> statement-breakpoint
CREATE INDEX "idx_status_history_timestamp" ON "connector_status_history" USING btree ("status_timestamp");--> statement-breakpoint
CREATE INDEX "idx_status_history_received_at" ON "connector_status_history" USING btree ("received_at");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_local_auth_list_charge_point_id_tag" ON "local_auth_list" USING btree ("charge_point_id","id_tag");--> statement-breakpoint
CREATE INDEX "idx_meter_value_transaction_id" ON "meter_value" USING btree ("transaction_id");--> statement-breakpoint
CREATE INDEX "idx_meter_value_connector_id" ON "meter_value" USING btree ("connector_id");--> statement-breakpoint
CREATE INDEX "idx_meter_value_timestamp" ON "meter_value" USING btree ("timestamp");--> statement-breakpoint
CREATE INDEX "idx_reservation_charge_point_id" ON "reservation" USING btree ("charge_point_id");--> statement-breakpoint
CREATE INDEX "idx_reservation_status" ON "reservation" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_reservation_expiry_date" ON "reservation" USING btree ("expiry_date");--> statement-breakpoint
CREATE INDEX "idx_transaction_charge_point_id" ON "transaction" USING btree ("charge_point_id");--> statement-breakpoint
CREATE INDEX "idx_transaction_connector_id" ON "transaction" USING btree ("connector_id");--> statement-breakpoint
CREATE INDEX "idx_transaction_id_tag" ON "transaction" USING btree ("id_tag");--> statement-breakpoint
CREATE INDEX "idx_transaction_start_timestamp" ON "transaction" USING btree ("start_timestamp");