-- Admin rolü ekle
ALTER TABLE profiles ADD COLUMN IF NOT EXISTS role TEXT DEFAULT 'user' CHECK (role IN ('user', 'admin'));

-- Seyahat planları tablosu
CREATE TABLE IF NOT EXISTS trips (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
  title TEXT NOT NULL,
  description TEXT,
  destination TEXT,
  start_date DATE,
  end_date DATE,
  cover_image TEXT,
  is_public BOOLEAN DEFAULT false,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Seyahat günleri tablosu
CREATE TABLE IF NOT EXISTS trip_days (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  trip_id UUID REFERENCES trips(id) ON DELETE CASCADE,
  day_number INTEGER NOT NULL,
  date DATE,
  notes TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Yerler tablosu
CREATE TABLE IF NOT EXISTS places (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT NOT NULL,
  description TEXT,
  type TEXT,
  address TEXT,
  city TEXT,
  country TEXT,
  latitude DECIMAL(10, 8),
  longitude DECIMAL(11, 8),
  rating DECIMAL(2, 1),
  image_url TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Seyahat günlerine eklenen yerler
CREATE TABLE IF NOT EXISTS trip_places (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  trip_day_id UUID REFERENCES trip_days(id) ON DELETE CASCADE,
  place_id UUID REFERENCES places(id) ON DELETE CASCADE,
  order_index INTEGER,
  duration TEXT,
  notes TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- RLS politikaları - Trips
ALTER TABLE trips ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Kullanıcılar kendi seyahatlerini görebilir"
  ON trips FOR SELECT
  USING (auth.uid() = user_id OR is_public = true);

CREATE POLICY "Kullanıcılar kendi seyahatlerini oluşturabilir"
  ON trips FOR INSERT
  WITH CHECK (auth.uid() = user_id);

CREATE POLICY "Kullanıcılar kendi seyahatlerini güncelleyebilir"
  ON trips FOR UPDATE
  USING (auth.uid() = user_id);

CREATE POLICY "Kullanıcılar kendi seyahatlerini silebilir"
  ON trips FOR DELETE
  USING (auth.uid() = user_id);

CREATE POLICY "Adminler tüm seyahatleri görebilir"
  ON trips FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM profiles WHERE id = auth.uid() AND role = 'admin'
  ));

CREATE POLICY "Adminler tüm seyahatleri silebilir"
  ON trips FOR DELETE
  USING (EXISTS (
    SELECT 1 FROM profiles WHERE id = auth.uid() AND role = 'admin'
  ));

-- RLS politikaları - Trip Days
ALTER TABLE trip_days ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Kullanıcılar kendi seyahat günlerini yönetebilir"
  ON trip_days FOR ALL
  USING (EXISTS (
    SELECT 1 FROM trips WHERE trips.id = trip_days.trip_id AND trips.user_id = auth.uid()
  ));

CREATE POLICY "Adminler tüm seyahat günlerini görebilir"
  ON trip_days FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM profiles WHERE id = auth.uid() AND role = 'admin'
  ));

-- RLS politikaları - Places
ALTER TABLE places ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Herkes yerleri görebilir"
  ON places FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "Adminler yerleri yönetebilir"
  ON places FOR ALL
  USING (EXISTS (
    SELECT 1 FROM profiles WHERE id = auth.uid() AND role = 'admin'
  ));

-- RLS politikaları - Trip Places
ALTER TABLE trip_places ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Kullanıcılar kendi seyahat yerlerini yönetebilir"
  ON trip_places FOR ALL
  USING (EXISTS (
    SELECT 1 FROM trip_days 
    JOIN trips ON trips.id = trip_days.trip_id 
    WHERE trip_days.id = trip_places.trip_day_id AND trips.user_id = auth.uid()
  ));

-- Admin kontrolü için fonksiyon
CREATE OR REPLACE FUNCTION is_admin()
RETURNS BOOLEAN AS $$
BEGIN
  RETURN EXISTS (
    SELECT 1 FROM profiles WHERE id = auth.uid() AND role = 'admin'
  );
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Profiller için admin okuma politikası
CREATE POLICY "Adminler tüm profilleri görebilir"
  ON profiles FOR SELECT
  USING (is_admin());

CREATE POLICY "Adminler profilleri güncelleyebilir"
  ON profiles FOR UPDATE
  USING (is_admin());

-- Updated_at trigger'ları
CREATE TRIGGER on_trip_updated
  BEFORE UPDATE ON trips
  FOR EACH ROW EXECUTE FUNCTION public.handle_updated_at();

CREATE TRIGGER on_place_updated
  BEFORE UPDATE ON places
  FOR EACH ROW EXECUTE FUNCTION public.handle_updated_at();
