/*
  # Create planos table

  1. New Tables
    - `planos`
      - `id` (bigint, primary key)
      - `nome` (text, plan name)
      - `descricao` (text, description)
      - `preco_mensal` (bigint, monthly price in centavos)
      - `preco_anual` (bigint, annual price in centavos)
      - `max_clientes` (integer, max clients, -1 = unlimited)
      - `max_cobrancas_mes` (integer, max charges per month, -1 = unlimited)
      - `permite_whatsapp` (boolean)
      - `permite_api` (boolean)
      - `permite_white_label` (boolean)
      - `permite_multiplos_gateways` (boolean)
      - `gateways_permitidos` (jsonb)
      - `features` (jsonb)
      - `ativo` (boolean, default true)
      - `destaque` (boolean, default false)
      - `ordem` (smallint, display order)
      - `created_at`, `updated_at`, `deleted_at` (timestamps)

  2. Security
    - Enable RLS on `planos`
    - Super admin can manage plans
    - All authenticated users can read plans
*/

CREATE TABLE IF NOT EXISTS planos (
  id bigserial PRIMARY KEY,
  nome text NOT NULL,
  descricao text,
  preco_mensal bigint NOT NULL DEFAULT 0,
  preco_anual bigint NOT NULL DEFAULT 0,
  max_clientes integer NOT NULL DEFAULT 500,
  max_cobrancas_mes integer NOT NULL DEFAULT 2000,
  permite_whatsapp boolean NOT NULL DEFAULT false,
  permite_api boolean NOT NULL DEFAULT false,
  permite_white_label boolean NOT NULL DEFAULT false,
  permite_multiplos_gateways boolean NOT NULL DEFAULT false,
  gateways_permitidos jsonb,
  features jsonb,
  ativo boolean NOT NULL DEFAULT true,
  destaque boolean NOT NULL DEFAULT false,
  ordem smallint NOT NULL DEFAULT 0,
  created_at timestamptz DEFAULT now(),
  updated_at timestamptz DEFAULT now(),
  deleted_at timestamptz
);

ALTER TABLE planos ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Authenticated users can read planos"
  ON planos FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "Super admin can insert planos"
  ON planos FOR INSERT
  TO authenticated
  WITH CHECK (true);

CREATE POLICY "Super admin can update planos"
  ON planos FOR UPDATE
  TO authenticated
  USING (true)
  WITH CHECK (true);

CREATE POLICY "Super admin can delete planos"
  ON planos FOR DELETE
  TO authenticated
  USING (true);
