CREATE TABLE IF NOT EXISTS public.requests (
id bigserial PRIMARY KEY,
ref_num text UNIQUE NOT NULL,
status text DEFAULT 'pending',
product_name text DEFAULT '',
company text DEFAULT '',
crn text DEFAULT '',
gov text DEFAULT '',
wilaya text DEFAULT '',
phone text DEFAULT '',
email text DEFAULT '',
riada text DEFAULT 'no',
national text DEFAULT 'no',
riada_file text DEFAULT '',
national_file text DEFAULT '',
cert_file text DEFAULT '',
projects jsonb DEFAULT '[]'::jsonb,
entities jsonb DEFAULT '[]'::jsonb,
products jsonb DEFAULT '[]'::jsonb,
reg_type text DEFAULT 'single',
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);
CREATE TABLE IF NOT EXISTS public.system_users (
id bigserial PRIMARY KEY,
username text UNIQUE NOT NULL,
password_hash text NOT NULL,
full_name text DEFAULT '',
email text DEFAULT '',
role text DEFAULT 'staff',
is_active boolean DEFAULT true,
created_at timestamptz DEFAULT now(),
last_login timestamptz
);
INSERT INTO public.system_users (username, password_hash, full_name, role)
VALUES ('admin', '1234', 'مدير النظام', 'admin')
ON CONFLICT (username) DO NOTHING;
ALTER TABLE public.requests ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.system_users ENABLE ROW LEVEL SECURITY;
CREATE POLICY "allow_all_requests" ON public.requests FOR ALL TO anon USING (true) WITH CHECK (true);
CREATE POLICY \"allow_all_users\" ON public.system_users FOR ALL TO anon USING (true) WITH CHECK (true);
-- Storage bucket for attachments
INSERT INTO storage.buckets (id, name, public)
VALUES ('attachments', 'attachments', true)
ON CONFLICT (id) DO NOTHING;
DROP POLICY IF EXISTS \"allow_upload_attachments\" ON storage.objects;
CREATE POLICY \"allow_upload_attachments\" ON storage.objects
FOR INSERT TO anon WITH CHECK (bucket_id = 'attachments');
DROP POLICY IF EXISTS \"allow_read_attachments\" ON storage.objects;
CREATE POLICY \"allow_read_attachments\" ON storage.objects
FOR SELECT TO anon USING (bucket_id = 'attachments');
-- Contractor profiles table
CREATE TABLE IF NOT EXISTS public.contractor_profiles (
id bigserial PRIMARY KEY,
crn text UNIQUE NOT NULL,
company_name text DEFAULT '',
email text DEFAULT '',
phone text DEFAULT '',
project_name text DEFAULT '',
created_at timestamptz DEFAULT now()
);
ALTER TABLE public.contractor_profiles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "allow_all_contractor_profiles" ON public.contractor_profiles FOR ALL TO anon USING (true) WITH CHECK (true);
-- Contractor orders table
CREATE TABLE IF NOT EXISTS public.contractor_orders (
id bigserial PRIMARY KEY,
order_ref text UNIQUE NOT NULL,
crn text NOT NULL,
contractor_name text DEFAULT '',
contractor_email text DEFAULT '',
contractor_phone text DEFAULT '',
project_name text DEFAULT '',
items jsonb DEFAULT '[]'::jsonb,
items_count integer DEFAULT 0,
total_amount numeric(12,3) DEFAULT 0,
status text DEFAULT 'pending',
created_at timestamptz DEFAULT now()
);
ALTER TABLE public.contractor_orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY "allow_all_contractor_orders" ON public.contractor_orders FOR ALL TO anon USING (true) WITH CHECK (true);