CREATE TABLE "account" (
	"id" text PRIMARY KEY NOT NULL,
	"account_id" text NOT NULL,
	"provider_id" text NOT NULL,
	"user_id" text NOT NULL,
	"access_token" text,
	"refresh_token" text,
	"id_token" text,
	"access_token_expires_at" timestamp,
	"refresh_token_expires_at" timestamp,
	"scope" text,
	"password" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp NOT NULL
);
--> statement-breakpoint
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 "session" (
	"id" text PRIMARY KEY NOT NULL,
	"expires_at" timestamp NOT NULL,
	"token" text NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp NOT NULL,
	"ip_address" text,
	"user_agent" text,
	"user_id" text NOT NULL,
	"impersonated_by" text,
	CONSTRAINT "session_token_unique" UNIQUE("token")
);
--> statement-breakpoint
CREATE TABLE "user" (
	"id" text PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"email" text NOT NULL,
	"email_verified" boolean DEFAULT false NOT NULL,
	"image" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"role" text,
	"banned" boolean DEFAULT false,
	"ban_reason" text,
	"ban_expires" timestamp,
	"username" text,
	"display_username" text,
	CONSTRAINT "user_email_unique" UNIQUE("email"),
	CONSTRAINT "user_username_unique" UNIQUE("username")
);
--> statement-breakpoint
CREATE TABLE "verification" (
	"id" text PRIMARY KEY NOT NULL,
	"identifier" text NOT NULL,
	"value" text NOT NULL,
	"expires_at" timestamp NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() 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,
	"fee_per_kwh" integer DEFAULT 0 NOT NULL,
	"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,
	"balance" integer DEFAULT 0 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 "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,
	"charge_amount" integer,
	"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 "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> 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");