-- =============================================================================
-- AgenciaPsi — Tables — Compromissos Determinados
-- =============================================================================
-- determined_commitments, determined_commitment_fields,
-- commitment_services, commitment_time_logs
-- =============================================================================

CREATE TABLE public.determined_commitments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    created_by uuid,
    is_native boolean DEFAULT false NOT NULL,
    native_key text,
    is_locked boolean DEFAULT false NOT NULL,
    active boolean DEFAULT true NOT NULL,
    name text NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    bg_color text,
    text_color text
);



CREATE TABLE public.determined_commitment_fields (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    commitment_id uuid NOT NULL,
    key text NOT NULL,
    label text NOT NULL,
    field_type public.determined_field_type DEFAULT 'text'::public.determined_field_type NOT NULL,
    required boolean DEFAULT false NOT NULL,
    sort_order 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
);



CREATE TABLE public.commitment_services (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    commitment_id uuid NOT NULL,
    service_id uuid NOT NULL,
    quantity integer DEFAULT 1 NOT NULL,
    unit_price numeric(10,2) NOT NULL,
    discount_pct numeric(5,2) DEFAULT 0,
    discount_flat numeric(10,2) DEFAULT 0,
    final_price numeric(10,2) NOT NULL,
    created_at timestamp with time zone DEFAULT now(),
    CONSTRAINT commitment_services_disc_flat_chk CHECK ((discount_flat >= (0)::numeric)),
    CONSTRAINT commitment_services_disc_pct_chk CHECK (((discount_pct >= (0)::numeric) AND (discount_pct <= (100)::numeric))),
    CONSTRAINT commitment_services_final_price_chk CHECK ((final_price >= (0)::numeric)),
    CONSTRAINT commitment_services_quantity_chk CHECK ((quantity > 0))
);


ALTER TABLE public.commitment_services OWNER TO supabase_admin;

--
-- Name: commitment_time_logs; Type: TABLE; Schema: public; Owner: supabase_admin
--

CREATE TABLE public.commitment_time_logs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    commitment_id uuid NOT NULL,
    calendar_event_id uuid,
    source public.commitment_log_source DEFAULT 'manual'::public.commitment_log_source NOT NULL,
    started_at timestamp with time zone NOT NULL,
    ended_at timestamp with time zone NOT NULL,
    minutes integer NOT NULL,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);


