import React, { useState } from "react";
import { ScrollView, View, TouchableOpacity } from "react-native";
import { Text } from "~/components/ui/text";
import BackButton from "~/components/ui/backButton";
import ScreenWrapper from "~/components/ui/ScreenWrapper";

type TabType = "terms" | "privacy";

export default function Terms() {
  const [activeTab, setActiveTab] = useState<TabType>("terms");

  return (
    <ScreenWrapper edges={[]}>
      <BackButton />

      <View className="flex-1 bg-white">
        {/* Header */}
        <View className="px-5 pt-8 pb-4">
          <Text className="text-3xl font-dmsans-bold text-gray-900">Legal</Text>
        </View>

        {/* Tabs */}
        <View className="flex-row px-5 mb-6">
          <TouchableOpacity
            onPress={() => setActiveTab("terms")}
            className="flex-1 mr-2"
            activeOpacity={0.7}
          >
            <View
              className={`pb-3 border-b-2 ${
                activeTab === "terms" ? "border-primary" : "border-gray-200"
              }`}
            >
              <Text
                className={`text-base font-dmsans-medium text-center ${
                  activeTab === "terms" ? "text-gray-900" : "text-gray-400"
                }`}
              >
                Terms & Conditions
              </Text>
            </View>
          </TouchableOpacity>

          <TouchableOpacity
            onPress={() => setActiveTab("privacy")}
            className="flex-1 ml-2"
            activeOpacity={0.7}
          >
            <View
              className={`pb-3 border-b-2 ${
                activeTab === "privacy" ? "border-primary" : "border-gray-200"
              }`}
            >
              <Text
                className={`text-base font-dmsans-medium text-center ${
                  activeTab === "privacy" ? "text-gray-900" : "text-gray-400"
                }`}
              >
                Privacy Policy
              </Text>
            </View>
          </TouchableOpacity>
        </View>

        {/* Content */}
        <ScrollView
          showsVerticalScrollIndicator={false}
          className="flex-1 px-5"
        >
          {activeTab === "terms" ? <TermsContent /> : <PrivacyContent />}
        </ScrollView>
      </View>
    </ScreenWrapper>
  );
}

function TermsContent() {
  return (
    <View className="pb-8">
      <Text className="text-2xl font-dmsans-bold text-gray-900 mb-6">
        Terms & Conditions
      </Text>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          1. Usage Rights
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          You are granted a limited, non-transferable license to use the app for
          personal or commercial reading purposes. Unauthorized reproduction or
          distribution of content is prohibited.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          2. User Responsibilities
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          You agree to use the app ethically, without engaging in activities
          that disrupt or harm the platform, including unauthorized access or
          data scraping.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          3. Purchases & Subscriptions
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          Some content may require a paid subscription or one-time purchase.
          Payments are processed securely, and refunds will be handled according
          to our refund policy.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          4. Account Security
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          You are responsible for maintaining the confidentiality of your
          account credentials. Notify us immediately of any unauthorized access
          or security breaches.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          5. Service Availability
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We strive to maintain service availability but do not guarantee
          uninterrupted access. Scheduled maintenance and updates may
          temporarily affect service.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          6. Limitation of Liability
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          Amba Pay is not liable for any indirect, incidental, or consequential
          damages arising from your use of the service. Our total liability is
          limited to the amount you paid for the service.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          7. Modifications to Terms
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We reserve the right to modify these terms at any time. Continued use
          of the service after changes constitutes acceptance of the modified
          terms.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          8. Termination
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We may suspend or terminate your account for violations of these
          terms. You may also close your account at any time through the app
          settings.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-sm font-dmsans text-gray-400 italic">
          Last updated: December 2024
        </Text>
      </View>
    </View>
  );
}

function PrivacyContent() {
  return (
    <View className="pb-8">
      <Text className="text-2xl font-dmsans-bold text-gray-900 mb-6">
        Privacy Policy
      </Text>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          1. Information We Collect
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We collect information you provide directly, such as your name, email,
          phone number, and payment information. We also collect usage data,
          device information, and location data when you use our services.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          2. How We Use Your Information
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          Your information is used to provide and improve our services, process
          transactions, send notifications, prevent fraud, and comply with legal
          obligations. We do not sell your personal information to third
          parties.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          3. Data Security
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We implement industry-standard security measures to protect your data,
          including encryption, secure servers, and regular security audits.
          However, no method of transmission over the internet is 100% secure.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          4. Information Sharing
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We may share your information with service providers who help us
          operate our platform, comply with legal requirements, or protect our
          rights. We require these parties to maintain confidentiality.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          5. Your Rights
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          You have the right to access, correct, or delete your personal
          information. You can also opt out of marketing communications and
          request data portability. Contact us to exercise these rights.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          6. Cookies & Tracking
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We use cookies and similar technologies to enhance your experience,
          analyze usage patterns, and deliver personalized content. You can
          manage cookie preferences in your device settings.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          7. Children's Privacy
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          Our services are not intended for children under 13. We do not
          knowingly collect personal information from children. If we discover
          such data, we will delete it promptly.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-lg font-dmsans-bold text-gray-900 mb-3">
          8. Changes to Privacy Policy
        </Text>
        <Text className="text-base font-dmsans text-gray-600 leading-6">
          We may update this privacy policy periodically. We will notify you of
          significant changes via email or in-app notification. Your continued
          use constitutes acceptance of the updated policy.
        </Text>
      </View>

      <View className="mb-6">
        <Text className="text-sm font-dmsans text-gray-400 italic">
          Last updated: December 2024
        </Text>
      </View>
    </View>
  );
}
