---
// src/pages/resources/kubernetes.astro
import BaseLayout from '../../layouts/BaseLayout.astro';
import Header from '../../components/Header.astro';
import Footer from '../../components/Footer.astro';
// Assuming card animations are needed if resource cards are used
import '../../styles/card-animations.css';

const title = "Kubernetes Resources | LaForceIT";
const description = "A collection of Kubernetes manifests, deployment configurations, and best practices for container orchestration.";

// Placeholder data - replace with actual data fetching or static data
const yamlManifests = [
  {
    title: "Deployment",
    description: "Standard Deployment with resource limits, probes, and environment variables",
    filename: "deployment.yaml",
    code: `apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-application
  namespace: production
  labels:
    app: web-application
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-application
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: web-application
        tier: frontend
    spec:
      containers:
      - name: web-application
        image: nginx:1.21-alpine
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: http
        resources:
          limits:
            cpu: "500m"
            memory: "512Mi"
          requests:
            cpu: "100m"
            memory: "128Mi"
        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: http
          initialDelaySeconds: 5
          periodSeconds: 5
        env:
        - name: NODE_ENV
          value: "production"
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d
      volumes:
      - name: config-volume
        configMap:
          name: nginx-config`,
    tags: ["deployment", "production", "nginx", "probes"],
    lastUpdated: "April 24, 2025",
    docLink: "#" // Replace with actual link
  },
  {
    title: "StatefulSet",
    description: "StatefulSet configuration for database deployments with persistent volumes",
    filename: "statefulset.yaml",
    code: `apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-db
  namespace: database
  labels:
    app: postgres-db
spec:
  serviceName: "postgres"
  replicas: 3
  selector:
    matchLabels:
      app: postgres-db
  template:
    metadata:
      labels:
        app: postgres-db
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: postgres
        image: postgres:14.2
        ports:
        - containerPort: 5432
          name: postgres
        env:
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: password
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "longhorn" # Example storage class
      resources:
        requests:
          storage: 10Gi`,
    tags: ["statefulset", "database", "postgres", "persistentvolume"],
    lastUpdated: "April 23, 2025",
    docLink: "#" // Replace with actual link
  }
  // Add more YAML manifest objects here
];

const helmCharts = [ /* Placeholder for Helm Chart data */ ];
const operators = [ /* Placeholder for Operator data */ ];
const bestPractices = [ /* Placeholder for Best Practice data */ ];

---

<BaseLayout {title} {description}>
  <Header slot="header" />

  <div class="container">
    <div class="page-header">
      <h1>Kubernetes Resources</h1>
      <div class="header-accent"></div>
    </div>

    <div class="resources-container">
      <div class="resources-tabs">
        <button class="resources-tab active" data-tab="yaml-manifests">YAML Manifests</button>
        <button class="resources-tab" data-tab="helm-charts">Helm Charts</button>
        <button class="resources-tab" data-tab="operators">Operators</button>
        <button class="resources-tab" data-tab="best-practices">Best Practices</button>
      </div>

      <!-- YAML Manifests Tab Content -->
      <div class="tab-content active" id="yaml-manifests">
        <h2 class="section-title">Kubernetes YAML Manifests</h2>
        <p class="section-description">
          A collection of production-ready Kubernetes manifest examples for various workload types and configuration patterns.
        </p>

        <div class="resources-grid">
          {yamlManifests.map(manifest => (
            <div class="resource-card">
              <div class="resource-header">
                <div class="resource-icon">
                  {/* Placeholder Icon - Consider dynamic icons based on kind */}
                  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                    <polyline points="14 2 14 8 20 8"></polyline>
                    <line x1="16" y1="13" x2="8" y2="13"></line>
                    <line x1="16" y1="17" x2="8" y2="17"></line>
                    <polyline points="10 9 9 9 8 9"></polyline>
                  </svg>
                </div>
                <div>
                  <h3 class="resource-title">{manifest.title}</h3>
                  <p class="resource-description">{manifest.description}</p>
                </div>
              </div>
              <div class="resource-body">
                <div class="code-preview">
                  <div class="code-header">
                    <span class="filename">{manifest.filename}</span>
                    <div class="code-actions">
                      <button class="code-action copy-button">Copy</button>
                      {/* <button class="code-action">Download</button> */}
                    </div>
                  </div>
                  <pre class="code-block"><code>{manifest.code}</code></pre>
                </div>
                <div class="tags">
                  {manifest.tags.map(tag => <span class="tag">{tag}</span>)}
                </div>
              </div>
              <div class="resource-footer">
                <span class="last-updated">Last updated: {manifest.lastUpdated}</span>
                <a href={manifest.docLink} class="resource-link" target="_blank" rel="noopener noreferrer">
                  View Documentation
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <line x1="5" y1="12" x2="19" y2="12"></line>
                    <polyline points="12 5 19 12 12 19"></polyline>
                  </svg>
                </a>
              </div>
            </div>
          ))}
        </div>
      </div>

      <!-- Helm Charts Tab Content (Placeholder) -->
      <div class="tab-content" id="helm-charts">
        <h2 class="section-title">Helm Charts</h2>
        <p class="section-description">Curated Helm charts for deploying common applications and services on Kubernetes.</p>
        <div class="resources-grid">
          <p>Helm chart resources coming soon...</p>
        </div>
      </div>

      <!-- Operators Tab Content (Placeholder) -->
      <div class="tab-content" id="operators">
        <h2 class="section-title">Kubernetes Operators</h2>
        <p class="section-description">Examples and guides for using Kubernetes Operators to automate application lifecycle management.</p>
        <div class="resources-grid">
          <p>Operator resources coming soon...</p>
        </div>
      </div>

      <!-- Best Practices Tab Content (Placeholder) -->
      <div class="tab-content" id="best-practices">
        <h2 class="section-title">Kubernetes Best Practices</h2>
        <p class="section-description">Tips, tricks, and best practices for managing Kubernetes clusters effectively and securely.</p>
        <ul class="best-practices-list">
          <li class="best-practice-item">
            <div class="best-practice-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>
            </div>
            <div class="best-practice-content">
              <h3>Security Hardening</h3>
              <p>Implement Role-Based Access Control (RBAC), Network Policies, and Secrets Management.</p>
            </div>
          </li>
           <li class="best-practice-item">
            <div class="best-practice-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="10" x2="6" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="18" y1="18" x2="6" y2="18"></line></svg>
            </div>
            <div class="best-practice-content">
              <h3>Resource Management</h3>
              <p>Define resource requests and limits for all workloads to ensure stability and fair resource allocation.</p>
            </div>
          </li>
          <!-- Add more best practices -->
        </ul>
      </div>
    </div>
  </div>

  <Footer slot="footer" />
</BaseLayout>

<style>
  /* Page-specific styles - Keep minimal, rely on global styles */
  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
  }

  .page-header {
    margin: 3rem 0 4rem;
    position: relative;
  }

  h1 {
    font-size: var(--font-size-4xl);
    background: linear-gradient(90deg, var(--accent-secondary), var(--accent-primary));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    display: inline-block;
    margin-bottom: 0.5rem;
  }

  .header-accent {
    width: 80px;
    height: 4px;
    background: linear-gradient(90deg, var(--accent-secondary), var(--accent-primary));
    border-radius: 2px;
  }

  .resources-container {
    padding: 1rem 0 4rem;
  }

  .resources-tabs {
    display: flex;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-primary);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    scrollbar-width: none; /* Hide scrollbar for Firefox */
  }
  .resources-tabs::-webkit-scrollbar {
      display: none; /* Hide scrollbar for Chrome, Safari, Edge */
  }


  .resources-tab {
    padding: 0.75rem 1.5rem;
    font-size: var(--font-size-base);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.3s ease;
    border-bottom: 2px solid transparent;
    white-space: nowrap; /* Prevent tabs from wrapping */
    flex-shrink: 0; /* Prevent tabs from shrinking */
  }

  .resources-tab.active {
    color: var(--accent-secondary);
    border-bottom-color: var(--accent-secondary);
    font-weight: 600;
  }

  .resources-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 1.5rem;
    margin-bottom: 3rem;
  }

  /* Use styles from card-animations.css for .resource-card */

  .resource-header {
    padding: 1.25rem;
    border-bottom: 1px solid var(--border-secondary);
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .resource-icon {
    color: var(--accent-secondary);
    flex-shrink: 0;
  }
   .resource-icon svg {
     width: 24px;
     height: 24px;
   }

  .resource-title {
    font-size: var(--font-size-xl);
    color: var(--text-primary);
    margin-bottom: 0.25rem;
  }

  .resource-description {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin-top: 0.5rem;
  }

  .resource-body {
    padding: 1.25rem;
    flex-grow: 1; /* Ensure body takes remaining space */
    display: flex;
    flex-direction: column;
  }

  .code-preview {
    background: var(--bg-code);
    border-radius: 8px;
    overflow: hidden;
    margin-top: 1rem;
    border: 1px solid var(--border-secondary);
    flex-grow: 1; /* Allow preview to grow */
    display: flex;
    flex-direction: column;
    min-height: 200px; /* Ensure a minimum height */
  }

  .code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--border-secondary);
  }

  .filename {
    font-family: var(--font-mono);
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
  }

  .code-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .code-action {
    font-size: var(--font-size-xs);
    color: var(--text-secondary);
    background: rgba(30, 41, 59, 0.5);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
  }

  .code-action:hover {
    background: rgba(59, 130, 246, 0.2);
    color: var(--accent-secondary);
  }

  .code-block {
    margin: 0 !important; /* Override potential global styles */
    padding: 1rem !important;
    background: transparent !important;
    font-family: var(--font-mono) !important;
    font-size: var(--font-size-sm) !important;
    line-height: 1.6 !important;
    overflow: auto !important; /* Changed from overflow-x */
    color: var(--text-secondary) !important;
    flex-grow: 1; /* Allow code block to fill space */
    max-height: 300px; /* Limit height */
  }
   /* Ensure pre takes full height */
  .code-block pre {
      height: 100%;
      margin: 0;
      padding: 0;
  }

  .tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 1rem;
  }

  /* .tag styling is now in card-animations.css */

  .resource-footer {
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto; /* Push footer to bottom */
  }

  .last-updated {
    font-size: var(--font-size-xs);
    color: var(--text-tertiary);
  }

  .resource-link {
    font-size: var(--font-size-sm);
    color: var(--accent-secondary);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .resource-link:hover {
    text-decoration: underline;
  }

  .best-practices-list {
    list-style: none;
  }

  .best-practice-item {
    display: flex;
    gap: 1rem;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-secondary);
  }

  .best-practice-item:last-child {
    border-bottom: none;
  }

  .best-practice-icon {
    color: var(--accent-secondary);
    flex-shrink: 0;
    margin-top: 0.25rem; /* Align icon better */
  }
   .best-practice-icon svg {
     width: 20px;
     height: 20px;
   }

  .best-practice-content h3 {
    font-size: var(--font-size-lg);
    margin-bottom: 0.5rem;
    color: var(--text-primary);
  }

  .best-practice-content p {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
  }

  .section-title {
    font-size: var(--font-size-2xl);
    margin-bottom: 1.5rem;
    color: var(--text-primary);
  }

  .section-description {
    font-size: var(--font-size-lg);
    color: var(--text-secondary);
    margin-bottom: 2rem;
    max-width: 800px;
  }

  .tab-content {
    display: none;
  }

  .tab-content.active {
    display: block;
  }

  @media (max-width: 768px) {
    .resources-grid {
      grid-template-columns: 1fr;
    }

    h1 {
      font-size: var(--font-size-3xl);
    }

    .section-title {
      font-size: var(--font-size-xl);
    }

    .section-description {
      font-size: var(--font-size-base);
    }
  }
</style>

<script>
  document.addEventListener('DOMContentLoaded', () => {
    const tabs = document.querySelectorAll('.resources-tab');
    const tabContents = document.querySelectorAll('.tab-content');

    tabs.forEach(tab => {
      tab.addEventListener('click', () => {
        const targetTab = tab.getAttribute('data-tab');

        // Deactivate all tabs and content
        tabs.forEach(t => t.classList.remove('active'));
        tabContents.forEach(c => c.classList.remove('active'));

        // Activate the clicked tab and corresponding content
        tab.classList.add('active');
        const activeContent = document.getElementById(targetTab);
        if (activeContent) {
          activeContent.classList.add('active');
        }
      });
    });

    // Copy button functionality
    document.querySelectorAll('.copy-button').forEach(button => {
      button.addEventListener('click', (event) => {
        const codeBlock = event.target.closest('.code-preview').querySelector('.code-block code');
        if (codeBlock) {
          navigator.clipboard.writeText(codeBlock.textContent || '')
            .then(() => {
              event.target.textContent = 'Copied!';
              setTimeout(() => {
                event.target.textContent = 'Copy';
              }, 2000);
            })
            .catch(err => {
              console.error('Failed to copy text: ', err);
              event.target.textContent = 'Error';
               setTimeout(() => {
                event.target.textContent = 'Copy';
              }, 2000);
            });
        }
      });
    });
  });
</script>