\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: reseller_inner; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA reseller_inner;


ALTER SCHEMA reseller_inner OWNER TO postgres;

--
-- Name: authenticate(); Type: FUNCTION; Schema: reseller_inner; Owner: postgres
--

CREATE FUNCTION reseller_inner.authenticate() RETURNS text
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
  v_user_id TEXT; 
BEGIN
    RAISE LOG 'authenticate called';
    v_user_id := (SELECT user_id FROM reseller_inner.resellers WHERE api_key = get_header('apikey'));
    IF v_user_id IS NULL THEN
        RAISE sqlstate 'PT401' USING message = 'Unauthorized';
    END IF;
    RAISE LOG 'authenticate successful for user_id: %', v_user_id;
    RETURN v_user_id;
END $$;


ALTER FUNCTION reseller_inner.authenticate() OWNER TO postgres;

--
-- Name: bytes_to_gb(numeric); Type: FUNCTION; Schema: reseller_inner; Owner: postgres
--

CREATE FUNCTION reseller_inner.bytes_to_gb(b numeric) RETURNS bigint
    LANGUAGE sql
    AS $$
  SELECT (b / 1073741824)::bigint;
$$;


ALTER FUNCTION reseller_inner.bytes_to_gb(b numeric) OWNER TO postgres;

--
-- Name: conversion_rate(common.dataimpulsepool); Type: FUNCTION; Schema: reseller_inner; Owner: postgres
--

CREATE FUNCTION reseller_inner.conversion_rate(pool common.dataimpulsepool) RETURNS numeric
    LANGUAGE plpgsql
    AS $$
BEGIN
    CASE pool
        WHEN 'datacenter' THEN RETURN 0.5;
        WHEN 'residential_premium' THEN RETURN 4;
        WHEN 'mobile' THEN RETURN 2;
        WHEN 'residential' THEN RETURN 1;
        ELSE RAISE EXCEPTION 'Invalid pool: %', pool;
    END CASE;
END;
$$;


ALTER FUNCTION reseller_inner.conversion_rate(pool common.dataimpulsepool) OWNER TO postgres;

--
-- Name: ensure_subuser_exists(integer); Type: FUNCTION; Schema: reseller_inner; Owner: postgres
--

CREATE FUNCTION reseller_inner.ensure_subuser_exists(subuser_id integer) RETURNS TABLE(id integer, pool common.dataimpulsepool)
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- Look up subuser and verify ownership by matching reseller's API key from request header
    SELECT rs.id, rs.pool
    FROM reseller_inner.reseller_subusers rs 
    JOIN reseller_inner.resellers r on rs.reseller_id = r.id
    WHERE rs.id = subuser_id AND r.api_key = get_header('apikey')
    INTO id, pool;

    -- Raise error if subuser does not exist or does not belong to the authenticated reseller
    IF id IS NULL THEN
        RAISE sqlstate 'PT404' USING
            message = 'Subuser not found',
            detail = 'No subuser associated with the provided id';
    END IF;

    RETURN NEXT;
    RETURN;
END $$;


ALTER FUNCTION reseller_inner.ensure_subuser_exists(subuser_id integer) OWNER TO postgres;

--
-- Name: FUNCTION ensure_subuser_exists(subuser_id integer); Type: COMMENT; Schema: reseller_inner; Owner: postgres
--

COMMENT ON FUNCTION reseller_inner.ensure_subuser_exists(subuser_id integer) IS 'Checks that the subuser exists and belongs to the reseller identified by the API key in the request header.';


--
-- Name: gb_to_bytes(integer); Type: FUNCTION; Schema: reseller_inner; Owner: postgres
--

CREATE FUNCTION reseller_inner.gb_to_bytes(gb integer) RETURNS bigint
    LANGUAGE sql
    AS $$
  SELECT gb::BIGINT * 1073741824;
$$;


ALTER FUNCTION reseller_inner.gb_to_bytes(gb integer) OWNER TO postgres;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: reseller_subusers; Type: TABLE; Schema: reseller_inner; Owner: postgres
--

CREATE TABLE reseller_inner.reseller_subusers (
    id integer NOT NULL,
    reseller_id integer NOT NULL,
    pool common.dataimpulsepool NOT NULL
)
INHERITS (common.created_at, common.updated_at);


ALTER TABLE reseller_inner.reseller_subusers OWNER TO postgres;

--
-- Name: COLUMN reseller_subusers.id; Type: COMMENT; Schema: reseller_inner; Owner: postgres
--

COMMENT ON COLUMN reseller_inner.reseller_subusers.id IS 'Dataimpulse subuser id';


--
-- Name: resellers; Type: TABLE; Schema: reseller_inner; Owner: postgres
--

CREATE TABLE reseller_inner.resellers (
    id integer NOT NULL,
    user_id text NOT NULL,
    api_key character varying(255) DEFAULT encode(public.gen_random_bytes(32), 'hex'::text),
    traffic_amount numeric DEFAULT 0 NOT NULL
)
INHERITS (common.created_at, common.updated_at);


ALTER TABLE reseller_inner.resellers OWNER TO postgres;

--
-- Name: resellers_id_seq; Type: SEQUENCE; Schema: reseller_inner; Owner: postgres
--

CREATE SEQUENCE reseller_inner.resellers_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE reseller_inner.resellers_id_seq OWNER TO postgres;

--
-- Name: resellers_id_seq; Type: SEQUENCE OWNED BY; Schema: reseller_inner; Owner: postgres
--

ALTER SEQUENCE reseller_inner.resellers_id_seq OWNED BY reseller_inner.resellers.id;


--
-- Name: reseller_subusers created_at; Type: DEFAULT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.reseller_subusers ALTER COLUMN created_at SET DEFAULT now();


--
-- Name: reseller_subusers updated_at; Type: DEFAULT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.reseller_subusers ALTER COLUMN updated_at SET DEFAULT now();


--
-- Name: resellers created_at; Type: DEFAULT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers ALTER COLUMN created_at SET DEFAULT now();


--
-- Name: resellers updated_at; Type: DEFAULT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers ALTER COLUMN updated_at SET DEFAULT now();


--
-- Name: resellers id; Type: DEFAULT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers ALTER COLUMN id SET DEFAULT nextval('reseller_inner.resellers_id_seq'::regclass);


--
-- Name: reseller_subusers reseller_subusers_pkey; Type: CONSTRAINT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.reseller_subusers
    ADD CONSTRAINT reseller_subusers_pkey PRIMARY KEY (id);


--
-- Name: resellers resellers_pkey; Type: CONSTRAINT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers
    ADD CONSTRAINT resellers_pkey PRIMARY KEY (id);


--
-- Name: resellers resellers_user_id_key; Type: CONSTRAINT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers
    ADD CONSTRAINT resellers_user_id_key UNIQUE (user_id);


--
-- Name: reseller_subusers reseller_subusers_reseller_id_fkey; Type: FK CONSTRAINT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.reseller_subusers
    ADD CONSTRAINT reseller_subusers_reseller_id_fkey FOREIGN KEY (reseller_id) REFERENCES reseller_inner.resellers(id);


--
-- Name: resellers resellers_user_id_fkey; Type: FK CONSTRAINT; Schema: reseller_inner; Owner: postgres
--

ALTER TABLE ONLY reseller_inner.resellers
    ADD CONSTRAINT resellers_user_id_fkey FOREIGN KEY (user_id) REFERENCES legacy.all_user(user_id);


--
-- PostgreSQL database dump complete
--

