-- ClickDelivery V0.8 — Loader Studio, branding runtime and expanded demo profiles
-- Developed by LM Networks.

CREATE TABLE IF NOT EXISTS loading_animation_library (
  id text PRIMARY KEY,
  name text NOT NULL,
  asset_url text NOT NULL,
  asset_kind text NOT NULL CHECK(asset_kind IN ('gif','webp','png','jpg','svg')),
  source text NOT NULL DEFAULT 'lm_networks' CHECK(source IN ('lm_networks','organization')),
  organization_id text REFERENCES organizations(id) ON DELETE CASCADE,
  default_motion_preset text NOT NULL DEFAULT 'pulse',
  active boolean NOT NULL DEFAULT true,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  CHECK ((source='lm_networks' AND organization_id IS NULL) OR (source='organization' AND organization_id IS NOT NULL))
);
CREATE INDEX IF NOT EXISTS loading_animation_library_org_idx ON loading_animation_library(organization_id,active);

CREATE TABLE IF NOT EXISTS organization_loading_profiles (
  organization_id text PRIMARY KEY REFERENCES organizations(id) ON DELETE CASCADE,
  library_animation_id text REFERENCES loading_animation_library(id) ON DELETE SET NULL,
  custom_asset_url text,
  motion_preset text NOT NULL DEFAULT 'pulse' CHECK(motion_preset IN ('none','pulse','bounce','float','spin','zoom','shake','orbit')),
  duration_ms integer NOT NULL DEFAULT 1200 CHECK(duration_ms BETWEEN 250 AND 10000),
  scale numeric(5,2) NOT NULL DEFAULT 1 CHECK(scale BETWEEN 0.25 AND 3),
  background_color text NOT NULL DEFAULT '#ffffff',
  icon_color text,
  loop boolean NOT NULL DEFAULT true,
  updated_by text REFERENCES users(id) ON DELETE SET NULL,
  updated_at timestamptz NOT NULL DEFAULT now()
);

INSERT INTO loading_animation_library(id,name,asset_url,asset_kind,source,default_motion_preset,active)
VALUES
 ('loader_clickdelivery_rabbit','ClickDelivery Conejo','/assets/loaders/original/clickdelivery-rabbit.gif','gif','lm_networks','none',true),
 ('loader_clickdelivery_rabbit_alt','ClickDelivery Conejo Alternativo','/assets/loaders/original/clickdelivery-rabbit-alt.gif','gif','lm_networks','none',true),
 ('loader_pin','Ubicación','/assets/loaders/library/pin.svg','svg','lm_networks','bounce',true),
 ('loader_package','Paquete','/assets/loaders/library/package.svg','svg','lm_networks','float',true),
 ('loader_route','Ruta','/assets/loaders/library/route.svg','svg','lm_networks','pulse',true),
 ('loader_bolt','Express','/assets/loaders/library/bolt.svg','svg','lm_networks','zoom',true),
 ('loader_ring','Anillo','/assets/loaders/library/ring.svg','svg','lm_networks','spin',true)
ON CONFLICT(id) DO UPDATE SET name=EXCLUDED.name,asset_url=EXCLUDED.asset_url,asset_kind=EXCLUDED.asset_kind,active=true,updated_at=now();

-- Existing franchise organizations receive the official ClickDelivery loader.
INSERT INTO organization_loading_profiles(organization_id,library_animation_id,motion_preset,duration_ms,scale,background_color,loop,updated_at)
SELECT id,'loader_clickdelivery_rabbit','none',1200,1,'#ffffff',true,now()
FROM organizations WHERE mode='franchise'
ON CONFLICT(organization_id) DO NOTHING;

DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname='clickdelivery_app') THEN
    GRANT SELECT,INSERT,UPDATE,DELETE ON loading_animation_library,organization_loading_profiles TO clickdelivery_app;
  END IF;
END $$;

ALTER TABLE loading_animation_library ENABLE ROW LEVEL SECURITY;
ALTER TABLE organization_loading_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE loading_animation_library FORCE ROW LEVEL SECURITY;
ALTER TABLE organization_loading_profiles FORCE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS loading_animation_library_tenant_policy ON loading_animation_library;
CREATE POLICY loading_animation_library_tenant_policy ON loading_animation_library
  USING (
    source='lm_networks'
    OR current_setting('app.is_superadmin',true)='true'
    OR organization_id=current_setting('app.organization_id',true)
  )
  WITH CHECK (
    current_setting('app.is_superadmin',true)='true'
    OR organization_id=current_setting('app.organization_id',true)
  );

DROP POLICY IF EXISTS organization_loading_profiles_tenant_policy ON organization_loading_profiles;
CREATE POLICY organization_loading_profiles_tenant_policy ON organization_loading_profiles
  USING (
    current_setting('app.is_superadmin',true)='true'
    OR organization_id=current_setting('app.organization_id',true)
  )
  WITH CHECK (
    current_setting('app.is_superadmin',true)='true'
    OR organization_id=current_setting('app.organization_id',true)
  );

COMMENT ON TABLE loading_animation_library IS 'Biblioteca LM Networks y activos de carga propios de cada Marca Blanca.';
COMMENT ON TABLE organization_loading_profiles IS 'Configuración del loader por tenant. Franquicia ClickDelivery usa por defecto el conejo oficial; Marca Blanca puede personalizar.';
